0

ここにフォームを作成し、httpwebrequest を使用してデータを投稿しようとしました。データがこのスプレッドシートに送信されず、理由がわかりません

    private const string PostURI = "https://docs.google.com/spreadsheet/formResponse?formkey=dDYyM0MxQUYzZnAwTGFfeFA0VG9VVUE6MQ&ifq";

    static void Main(string[] args)
    {


        BeginGetRequestExample();            
        Console.Read();
    }

    private static void BeginGetRequestExample()
    {
        var request = (HttpWebRequest)HttpWebRequest.Create(PostURI);
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";
        request.BeginGetRequestStream(new AsyncCallback(GetRequestCallback), request);
    }

    private static void GetRequestCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        using (var postStream = request.EndGetRequestStream(asynchronousResult))
        {
            string postData = "entry.0.single=testdata";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            postStream.Write(byteArray, 0, postData.Length);
        }

    }
4

2 に答える 2

0

非同期ジョブのステータスを確認するためにチェックしているコードの場所はありません。ResetEvents を使用して、非同期作業が完了するのを確実に待つことができます。

宣言するManualResetEvent

private static ManualResetEvent workCompleted= new ManualResetEvent(false);

workCompleted.WaitOne()を BeginGetRequestExampleに追加します。これで、このメソッドは Asynchronous メソッドが完了するまで待機します。

 request.BeginGetRequestStream(new AsyncCallback(GetRequestCallback), request);
 workCompleted.WaitOne();//This will block the thread.

workCompleted.Set()を追加しますGetRequestCallback。 set() 呼び出しは、実行の完了を BeginGetRequestStream に通知します

 using (var postStream = request.EndGetRequestStream(asynchronousResult))
            {
                string postData = "entry.0.single=testdata";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                postStream.Write(byteArray, 0, postData.Length);
            }

 workCompleted.Set();//work completed,signal BeginGetRequestExample.
于 2012-10-29T08:55:04.733 に答える
0

私の経験から...あなたが投稿したデータはおそらく正しくないと思います。

これは生の HttpPost です

POST https://docs.google.com/spreadsheet/formResponse?formkey=dDYyM0MxQUYzZnAwTGFfeFA0VG9VVUE6MQ&ifq HTTP/1.1
Host: docs.google.com
Connection: keep-alive
Content-Length: 62
Cache-Control: max-age=0
Origin: https://docs.google.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
X-Chrome-Variations: CPO1yQEIl7bJAQietskBCKS2yQEIp7bJAQiptskBCLW2yQEIsIPKAQ==
Referer: https://docs.google.com/spreadsheet/viewform?formkey=dDYyM0MxQUYzZnAwTGFfeFA0VG9VVUE6MQ&ifq
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: PREF=ID=95ae5350cc16c940:U=28fe773a18c2dbe6:LD=en:TM=1345120351:LM=1345194034:GM=1:S=3Cn9QquJ5Czg34G0; __utma=184632636.1591015415.1346839842.1346839842.1346839842.1; __utmz=184632636.1346839842.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); NID=62=1rseSYPhjiIKnlAgPIthlh-KblNw8OVSOJuFL54wW2wX9HmucRXT83vYgz-JAUlgGuFv8BJTQutADeZvxtQnOnnUtOhckpRN6Q39zeRb9yKqCp2p1U-ZB3mhmkPwz4Iz0j3FHeGty9yDqOjlma5rG5GtgRyl6z7YakbICD7ckt11JJm3-jrFemtNg-wH1sqWZWiThPzb4R40Q3RAeohBgvU3-nJ38_VZDG9rA7pTVvQ7jw; HSID=AqYa-X7Si3Broi94c; SSID=AxGpbOiAWCleF5e9z; APISID=8Fgs9inkdycfWieu/AEg1bwhd3Aesfa3XR; SAPISID=G9xcUdWksRD2dzMM/AbmBuqsL_bNcslBTQ; WRITELY_SID=DQAAAGEBAACUg03i_4fbFZEg_gr-iMZ6zg_h5P-tqox77Gw_mLIqgQtDD0OyUGTw0Dn1p5plW1rQnqNuDYr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D9Db9vQWmoCRiterbKY67_4nJYRaaY69djDdpJUXpykAYq5XGCWoX89qQUbkZmixKUZU2dXIPgxdpnEwmSJNcspjuLfJWhRMRnIc4cGhV1osB2W33XriFWZQ-vIfUoIAygrM-a_zpK4-Bw0L4hym8aVdMv2pB9QwEzPFKzNBa7l3PLjs7dHWqu-btHnVNf_j9tZk0GqE3EjrsdSeJpTaRfwW0vBhdemHbPJtIq5DmdAAwNDV3_RM79iS6S1vyszAb_G51ZDWH1-1IR8FjbgTPJT2llGmwYKe9C63LK5_WzqenwoPQnJj5kwUEeBnqqs_9nKOD2cFxDhuVky_Z5AIuhVZAx2sUzgIG8uB1PQ

entry.0.single=abcdefg&pageNumber=0&backupCache=&submit=Submit

これは生の http 応答です

HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://docs.google.com
Content-Type: text/html; charset=utf-8
X-Robots-Tag: noindex, nofollow, nosnippet
Date: Mon, 29 Oct 2012 09:47:43 GMT
Expires: Mon, 29 Oct 2012 09:47:43 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Server: GSE
Content-Length: 1225

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Thanks!</title>

<link href='/static/spreadsheets/client/css/627030122-form_response_compiled.css' type='text/css' rel='stylesheet'>
<style type="text/css">
      .ss-logo {background:transparent url("https://ssl.gstatic.com/docs/spreadsheets/forms/sprites_2.png") no-repeat}
    </style></head>
<body><div class="ss-container"><div class="ss-resp-card"><div class="ss-confirmation">TestPost</div>
<div class="ss-custom-resp">เราได้บันทึกคำตอบของคุณไว้แล้ว </div>
<div class="ss-opt-actions">

</div>
<hr class="ss-break">
<div class="ss-footer"><div class="ss-footer-txt"><a class="ss-actions-link" href="https://docs.google.com/spreadsheet/viewform?formkey=dDYyM0MxQUYzZnAwTGFfeFA0VG9VVUE6MQ&amp;ifq">Submit another response</a>
|
<a class="ss-actions-link" href="https://docs.google.com/spreadsheet/newform?ref=submitpage">Create your own form</a></div>
<a class="ss-logo-link" href="https://docs.google.com/"><div class="ss-logo">&nbsp;&nbsp;</div></a></div></div></div></body></html>

おそらくあなたがしなければならないことは、正確な HttpPost を構築することです。

于 2012-10-29T09:54:22.493 に答える