调用 GetResponse() 时出现超时异常

发布于 2024-11-02 16:26:23 字数 1369 浏览 1 评论 0原文

我创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2024-11-09 16:26:23

您应该使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文