簡體   English   中英

Web服務在第3次點擊時請求超時

[英]Web service requests timeout on 3rd hit

我已經編寫了ac#應用程序,將我的數據從Blinksale遷移到Freeagent。 一切正常,我正在適當地進行各種Web服務調用,但是,當我批量運行該流程時,它將到達導入的第三張發票並超時。 在像

            Stream dataStream = request.GetRequestStream ();

現在,我已經聯系了非常出色的API提供程序(Freeagent),他們已經檢查了日志,並進行了前2次嘗試(每個嘗試包括3個單獨的調用),但是沒有第三個請求的跡象。 這將使該Web服務收到第8個請求,但是在此之前可能已經有很多人使用類似的代碼來獲取數據。

因此,我假設這與我的.net代碼有關。

事情太多了,我很難分享任何片段,但是本質上,我多次做同樣的事情。

注意:我已經檢查過,但不是第三個請求中的數據有問題(我以相同的結果跳過了它)

// 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 ();

您將顯式關閉所有內容-這意味着如果引發任何異常, 則不會關閉任何內容。 WebResponse和所有涉及的流使用using語句。 通常,“ Web請求在第三次嘗試時超時”的問題由於未正確關閉連接而導致的……並且看起來您正在關閉所有內容,但在成功的情況下,可能涉及到一些微妙之處。 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);

哇,代碼太多了。 嘗試這個:

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));
}

如果這不起作用,則是網絡和/或服務器問題。 您還可以通過將以下內容放入app / 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>

我遇到了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://example/post發出POST HTTP請求(在本例中為靜態ID);

您可能不需要格式化字符串中的整個data={} ,所以如果對您不起作用,請嘗試一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM