2012-11-05 58 views
4

我使用HttpWebRequest發佈數據時遇到問題。通過使用HttpWebRequest丟失發佈數據

有一個字符串(即key1=value1&key2=value2&key3=value3),我已經發布到一個網站(即www。*。com/edit),但是,我不知道爲什麼有時它沒有錯,但有時,第一個key=value1將會丟失,只有key2=value&key3=value3可以在HttpAnalyzer找到。

public static string SubmitData(string Url, string FormData, CookieContainer _Cc, string ContentType) 
     { 
      Stream RequestStream = null, ResponseStream = null; StreamReader Sr = null; 
      HttpWebRequest HRequest = (HttpWebRequest)WebRequest.Create(Url); 
      try 
      { 

       HRequest.CookieContainer = _Cc; 
       HRequest.Method = "POST"; 
       HRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"; 
       HRequest.ContentType = ContentType; 
       HRequest.ContentLength = FormData.Length; 
       //byte[] BFromData = new ASCIIEncoding().GetBytes(FormData); 
       byte[] BFromData = Encoding.ASCII.GetBytes(FormData); 
       BFromData = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, BFromData);//ascii → utf8 
       RequestStream = HRequest.GetRequestStream(); 
       RequestStream.Write(BFromData, 0, BFromData.Length); 
       //RequestStream.Write(utf8Bytes,0,utf8Bytes.Length); 
       HttpWebResponse HResponse = (HttpWebResponse)HRequest.GetResponse(); 
       ResponseStream = HResponse.GetResponseStream(); 
       Sr = new StreamReader(ResponseStream, Encoding.UTF8); 
       return Sr.ReadToEnd(); 
      } 
      catch 
      { 
       return ""; 
      } 
      finally 
      { 
       if (null != RequestStream) RequestStream.Close(); 
       if (null != ResponseStream) ResponseStream.Close(); 
       if (null != Sr) Sr.Close(); 
      } 
     } 
+0

請顯示您的代碼。 –

+2

您可以顯示您在其中發佈的示例鏈接嗎? – Rob

+0

@JonSkeet我現在更新了 – SheldonLou

回答

3

使用Fiddler看到該請求的樣子,當您單擊窗體上,然後嘗試使用這種方法,並修改您的需要爲您的要求等。

public static void PostDataAndDoSomething() 
     {    
      string URI = "http://www.something.com"; 
      //make your request payload 
      string requestBody = String.Format("{{'param1': {0}, 'param2': {1}}}",value1, value2); //json format 

      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI); //make request   

      // set request headers as you need 
      request.ContentType = "application/json; charset=UTF-8"; 
      request.Accept = "application/json, text/javascript; 
      request.Method = "POST";    
      request.UserAgent = ""; 
      request.Headers.Add("X-Requested-With", "XMLHttpRequest"); 
      using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) 
      { 
       writer.Write(requestBody); //write your request payload 
      } 

      WebResponse response = request.GetResponse();   
      string jsonData = String.Empty; 

      using (var reader = new StreamReader(response.GetResponseStream())) 
      { 
       jsonData = reader.ReadToEnd(); 
      }   
      response.Close(); 

      //do something with your data, deserialize, Regex etc.... 
     } 
+0

我試過了,但問題並沒有消失。 – SheldonLou

+0

@HelloWorld你可以給我你發佈數據的鏈接嗎? –

+0

這是一個需要登錄的站點,所以 – SheldonLou