[英]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.