我使用httpwebrequest和httpwebresponse分别发送请求和获取响应。 由于某种原因,我的连接在收到响应之前就会关闭。如何在使用httpwebrequest时保持连接直播?
这里是我的代码:
WebRequest webRequest = WebRequest.Create (uri);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes (parameters);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; //Count bytes to send
os = webRequest.GetRequestStream();
os.Write (bytes, 0, bytes.Length); //Send it
}
catch (WebException ex)
{
MessageBox.Show (ex.Message, "HttpPost: Request error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
try
{ // get the response
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{ return null; }
StreamReader sr = new StreamReader (webResponse.GetResponseStream());
return sr.ReadToEnd().Trim();
}
catch (WebException ex)
{
MessageBox.Show (ex.Message, "HttpPost: Response error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return null;
}
错误:
几乎看不到错误信息。如果连接超时,请检查您的Web服务器(或J2EE服务器)配置文件。 – exiter2000 2010-11-02 20:05:42
错误:远程服务器返回错误。(500)内部服务器错误。 – BumbleBee 2010-11-02 20:10:24
当我看着堆栈,我看到连接:关闭 – BumbleBee 2010-11-02 20:10:42