2009-04-20 32 views
8

我試圖在.NET Compact Framework上執行一個異步HTTP(S)POST,但我似乎無法讓它工作。在.NET Compact Framework中使用POST參數的異步WebRequest

下面是我在做什麼:

private void sendRequest(string url, string method, string postdata) { 
    WebRequest rqst = HttpWebRequest.Create(url); 
    CredentialCache creds = new CredentialCache(); 
    creds.Add(new Uri(url), "Basic", new NetworkCredential(this.Uname, this.Pwd)); 
    rqst.Credentials = creds; 
    rqst.Method = method; 
    if (!String.IsNullOrEmpty(postdata)) { 
     rqst.ContentType = "application/xml"; 
     byte[] byteData = UTF8Encoding.UTF8.GetBytes(postdata); 
     rqst.ContentLength = byteData.Length; 
     using (Stream postStream = rqst.GetRequestStream()) { 
      postStream.Write(byteData, 0, byteData.Length); 
      postStream.Close(); 
     } 
    } 
    ((HttpWebRequest)rqst).KeepAlive = false; 
    rqst.BeginGetResponse(DataLoadedCB, rqst); 
} 

private void DataLoadedCB(IAsyncResult result) { 
    WebRequest rqst = ((WebRequest)(((BCRqst)result.AsyncState).rqst)); 
    WebResponse rsps = rqst.EndGetResponse(result); 

    /* ETC...*/ 
} 

...但由於某種原因,我得到DataLoadedCB的第二行引發WebException:

「這一要求,需要進行身份驗證或數據的緩衝重定向成功「。

完全相同的代碼完美工作,當我做一個簡單的HTTP GET,但是當我拋出一些POST PARAMS,一切都失敗。

任何想法?

回答

12

我非常高興!我找到了我的問題的答案!

這個小行的伎倆:

((HttpWebRequest)rqst).AllowWriteStreamBuffering = true; 
+1

我想你的意思是添加此附近**((HttpWebRequest的)RQST).KeepAlive = FALSE; ** :) – 2014-10-24 20:40:43

相關問題