【发布时间】:2011-04-20 01:29:34
【问题描述】:
我创建了一个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;
}
}
}
【问题讨论】:
-
能否也显示请求代码?
-
请看问题中添加的请求代码
标签: c# webrequest getresponse