2010-11-17 73 views
1

我寫了一個c#應用程序,將我的數據從Blinksale遷移到Freeagent。這一切都工作正常,我正在適當的各種Web服務調用,但是當我運行一個批處理的進程,它獲得第三個發票上的導入和超時。在一個像像Web服務請求第三次點擊超時

  Stream dataStream = request.GetRequestStream(); 

現在我已經聯繫了API提供商(希捷FreeAgent),誰是很好的,他們已經檢查了日誌,第一2次嘗試(其中包括3個人的每一個電話),但沒有第三個請求的跡象。這將使得該Web服務的第8次請求成爲可能,但在此之前可能會有相當多的代碼使用類似的代碼來獲取數據。

因此,我認爲這是與我的.NET代碼。

因爲有這麼多事情發生,所以我很難分享任何片段,但基本上我多次做同樣的事情。

N.B.我已經檢查,它不是第三個請求中的數據(我跳過它的結果相同)

// Create a request using a URL that can receive a post. 
      WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx "); 
      // 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. 
      Stream dataStream = request.GetRequestStream(); 
      // Write the data to the request stream. 
      dataStream.Write (byteArray, 0, byteArray.Length); 
      // Close the Stream object. 
      dataStream.Close(); 
      // Get the response. 
      WebResponse response = request.GetResponse(); 
      // Display the status. 
      Console.WriteLine (((HttpWebResponse)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); 
      // Clean up the streams. 
      reader.Close(); 
      dataStream.Close(); 
      response.Close(); 
+0

您是否確認該請求將從您的代碼發出?這可能是需要研究的。 – 2010-11-17 17:36:20

回答

2

哇,這麼多的代碼。試試這個:

using (var client = new WebClient()) 
{ 
    var values = new NameValueCollection 
    { 
     { "key", "This is a test that posts this string to a Web server." } 
    }; 
    string url = "http://www.contoso.com/PostAccepter.aspx"; 
    byte[] result = client.UploadValues(url, values); 
    Console.WriteLine(Encoding.UTF8.GetString(result)); 
} 

如果這不起作用,這是一個網絡和/或服務器問題。你也可以通過添加如下在您的應用程序/ web.config中激活客戶端上的日誌確切地知道發生了什麼HTTP堆棧:

<system.diagnostics> 
    <sources> 
    <source name="System.Net" tracemode="protocolonly"> 
     <listeners> 
     <add name="System.Net" type="System.Diagnostics.TextWriterTraceListener" initializeData="network.log" /> 
     </listeners> 
    </source> 
    </sources> 

    <switches> 
    <add name="System.Net" value="Verbose"/> 
    </switches> 

    <trace autoflush="true" /> 
</system.diagnostics> 
+0

大聲笑代碼只是一個從我被用於我的代碼剪切,但我會在這個例子中沒有意義。我會給跟蹤想法去感謝和使用 – 2010-11-18 13:36:47

4

你明確地關閉一切 - 這意味着,如果有任何的例外是拋出,你不會被關閉的東西。使用using聲明爲WebResponse和所有涉及的流。通常情況下,「第三次嘗試的網絡請求超時」問題由於未正確關閉連接而導致 ......雖然看起來您正在關閉所有內容,但在成功案例中,可能會涉及一些微妙之處。 A using聲明更簡單。

因此,例如:

string responseText; 
using (WebResponse response = request.GetResponse()) 
{ 
    Console.WriteLine (((HttpWebResponse)response).StatusDescription); 
    using (Stream responseStream = response.GetResponseStream()) 
    using (StreamReader reader = new StreamReader(responseStream)) 
    { 
     responseText = reader.ReadToEnd(responseText); 
    } 
} 
Console.WriteLine(responseText); 
+0

感謝喬恩,我從MS例子:D將給它 – 2010-11-18 13:37:19

+0

這是這個真正的答案。我遇到了同樣的問題,並且圍繞WebResponse添加using語句修復了它。謝謝喬恩! – 2012-10-09 19:11:50

0

我是有由tigermain和感謝提到達林同樣的問題,我設法得到它與下面的解決方案工作:

using(WebClient client = new WebClient()){ 
     string url = "http://example.org/post"; 
     int id = 1; 
     string dataFormat = "data={{\"Id\":{0} }}"; 
     string dataString = string.Format (dataFormat, id); 
     byte[] data = ASCIIEncoding.ASCII.GetBytes(dataString); 
     client.UploadData(url, "POST", data); 
} 

這將使發送HTTP請求到http://example/post與你的數據(在這個例子中,只是一個靜態ID);

您的格式字符串中可能不需要整個data={},所以如果不適合您,請仔細閱讀。