2012-10-29 44 views
0

我在這裏創建form並嘗試使用httpwebrequest發佈數據。數據不會提交到這個spreadsheet,我不知道爲什麼使用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); 
     } 

    } 
+0

請定義'沒有工作'... –

+0

已經重新定義它 –

+0

你有666回購,這是令人毛骨悚然的D :: D:D大聲笑 – m4ngl3r

回答

0

無論在哪裏在你的代碼要檢查看異步job.You的狀態可以用ResetEvents以確保您等待異步工作,完成

聲明一個ManualResetEvent

private static ManualResetEvent workCompleted= new ManualResetEvent(false); 

添加workCompleted.WaitOne()您BeginGetRequestExample.Now此方法將等待Asynchronou s的方法來完成。

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

添加workCompleted.Set()GetRequestCallback .The集()調用將信號執行的完成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. 
+0

我應該在哪裏呢? –

+0

已做了編輯,請檢查。您需要使用手動重置事件。 –

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; SA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zzXs8OIHA; SID=DQAAAF8BAABm0OXzbbbSZWD3Qj4rTcK4dDifbWX0DEgZ3cBs_xxFxCJJj35PBe6ORIspSn-inbK3g4fU6e6OsedvVP11l_Rf9GOA-BucVEYOpL8f7LWSbybhRD9Db9vQWmoCRiterbKY67_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。

相關問題