开发者

Web service requests timeout on 3rd hit

I have written a c# app to migrate my data from Blinksale to Freeagent. This is all working fine and I am making the various web service calls suitably however when I run the process as a batch it gets to the 3rd invoice on the import and times out. On a like like

            Stream dataStream = request.GetRequestStream ();

Now I've contacted the API provider (Freeagent), who are very good, and they have checked the logs, and the first 2 attempts are made (which consist of 3 individual calls each) but there is no sign of the 3rd request. This would make it about the 8th request to this web service, but there may have been quite a few using similar code prior to this to get the data.

Therefore I am assuming it is something to do with my .net code.

Its hard for me to share any snippets as there is so much going on, but essentially I am doing the same thing as this multiple times.

N.B. I have checked and it isnt the data in the 3rd request thats in question (I skipped it with same result)

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


You're closing everything explicitly - which means that if any exceptions are thrown, you won't be closing things. Use using statements for the WebResponse and all the streams involved. Usually "web request times out on third attempt" problems are due to connections not being closed appropriately... and while it looks like you're closing everything, in the success case, there could be some subtlety involved. A using statement is simply more robust.

So for example:

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


Wow, so much code. Try this:

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

If this doesn't work it's a network and/or server problem. You could also activate logs on the client by putting the following in your app/web.config to know exactly what's happening on the HTTP stack:

<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>


I was having the same problem mentioned by tigermain and thanks to Darin I managed to get it working with the following solution:

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

This will make a POST HTTP request to http://example/post with your data (in this example case just a static id);

You might not need the whole data={} in your formatting string so play around with that if it doesn't work for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜