调用 GetResponse() 时出现超时异常
我创建一个 WebRequest
将一些 HTML 内容发布到另一个 Web 服务器。当我使用普通文本内容时,它可以工作,但是当我发布 HTML 内容时,我在调用 GetResponse()
时收到超时错误。
WebResponse response = request.GetResponse()
我怎样才能弄清楚这个问题? 我尝试为 WebException
添加错误处理程序,但无法捕获异常。
请求代码:
byte[] byteArray = Encoding.UTF8.GetBytes(sPostData);
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(httpUri);
// Set the 'Timeout' property in Milliseconds.
//request.Timeout = 20000;
// Set the ContentType property of the WebRequest.
request.ContentType = contentType;
// Set the Method property of the request to POST.
request.Method = postMethod;
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
using (Stream PostStream = request.GetRequestStream())
{
PostStream.Write(byteArray, 0, byteArray.Length);
}
// Get the response.
using (WebResponse response = request.GetResponse())
{
// Get the stream containing content returned by the server.
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
string responseFromServer = reader.ReadToEnd();
result = responseFromServer;
}
}
}
I create a WebRequest
to post some HTML content to another web server. When I use normal text content, it works, but when I post HTML content I get a time out error when invoking GetResponse()
.
WebResponse response = request.GetResponse()
How can I figure out the issue?
I tried adding an error handler for WebException
but I could not catch the exception.
Request code :
byte[] byteArray = Encoding.UTF8.GetBytes(sPostData);
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(httpUri);
// Set the 'Timeout' property in Milliseconds.
//request.Timeout = 20000;
// Set the ContentType property of the WebRequest.
request.ContentType = contentType;
// Set the Method property of the request to POST.
request.Method = postMethod;
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
using (Stream PostStream = request.GetRequestStream())
{
PostStream.Write(byteArray, 0, byteArray.Length);
}
// Get the response.
using (WebResponse response = request.GetResponse())
{
// Get the stream containing content returned by the server.
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
string responseFromServer = reader.ReadToEnd();
result = responseFromServer;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 Fiddler 或其他等效应用程序查看网络流量。这可能会向您显示服务器是否完全响应,或者可能响应较晚。
您还应该查看服务器日志或 Windows 事件日志 - 也许服务器不喜欢发送 HTML,并且正在记录错误。在这种情况下,它可能只是认为您正在尝试破解它,并且可能决定根本不回答您。
You should look at the network traffic using Fiddler or another equivalent application. This may show you whether the server is responding at all, or perhaps it is responding late.
You should also look at the server logs or the Windows Event Log - perhaps the server doesn't like being sent HTML, and is logging an error. In this case, it may simply decide that you are trying to hack it, and it may decide not to answer you at all.