简体   繁体   English

调用GetResponse()时发生超时异常

[英]Time out Exception when invoking GetResponse()

I create a WebRequest to post some HTML content to another web server. 我创建一个WebRequest以将一些HTML内容发布到另一个Web服务器。 When I use normal text content, it works, but when I post HTML content I get a time out error when invoking GetResponse() . 当我使用普通文本内容时,它可以工作,但是当我发布HTML内容时,调用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. 我尝试为WebException添加错误处理程序,但无法捕获异常。

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

You should look at the network traffic using Fiddler or another equivalent application. 您应该使用Fiddler或其他等效应用程序查看网络流量。 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. 您还应该查看服务器日志或Windows事件日志-也许服务器不喜欢发送HTML,并且正在记录错误。 In this case, it may simply decide that you are trying to hack it, and it may decide not to answer you at all. 在这种情况下,它可能会简单地决定您正在尝试对其进行破解,并且可能会决定根本不回答您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM