2012-11-11 40 views
0

我目前遇到問題。 我試圖將我的數據發佈到PHP文檔,但它沒有得到整個值。 中間的某處停止發佈。c#WebRequest不會發布整個數據長度

有誰知道問題出在哪裏? 該bytearray是7401長。這不能長久?

我的代碼如下:

public string RecieveData(string url, string postData = "") 
    { 

      WebRequest request = WebRequest.Create(url); 
      // If required by the server, set the credentials. 

      NetworkCredential nc = new NetworkCredential("user", "pass"); 
      Stream dataStream; 

      if (postData != "") 
      { 
       // Set the Method property of the request to POST. 
       request.Method = "POST"; 
       // Create POST data and convert it to a byte array.string postData = "This is a test that posts this string to a Web server."; 
       byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
       // Set the ContentType property of the WebRequest. 
       request.ContentType = "application/x-www-form-urlencoded"; 
       // Set the ContentLength property of the WebRequest. 
       request.ContentLength = byteArray.Length; 
       // Get the request stream. 
       dataStream = request.GetRequestStream(); 
       // Write the data to the request stream. 
       dataStream.Write(byteArray, 0, byteArray.Length); 
       // Close the Stream object. 
       dataStream.Close(); 
      } 

      request.Credentials = nc; 
      // Get the response. 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      // Display the status. 
      Console.WriteLine(response.StatusDescription); 
      // Get the stream containing content returned by the server. 
      dataStream = response.GetResponseStream(); 
      // Open the stream using a StreamReader for easy access. 
      StreamReader reader = new StreamReader(dataStream); 
      // Read the content. 
      string responseFromServer = reader.ReadToEnd(); 
      // Display the content. 
      Console.WriteLine(responseFromServer); 
      // Cleanup the streams and the response. 
      reader.Close(); 
      dataStream.Close(); 
      response.Close(); 

      return responseFromServer; 
     /* 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Er is iets fout gegaan met verbinden"); 
      return ""; 
     } 
     */ 

    } 
+0

是什麼讓你覺得它停止發佈?你有沒有使用Wireshark或類似的東西來看看網絡層面發生了什麼? –

回答

0

7401不會太長。我的猜測是你發佈的數據不是完全的URL編碼,例如,字節數組中的一個字符導致PhP解析器停止。確保你正在查看原始數據(例如使用Wireshark)。