2010-11-17 37 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={},所以如果不适合您,请仔细阅读。