我正在尝试在 asp.net4.0 c# 中进行发布请求。但我在阅读响应时收到错误。
错误:异常:基础连接已关闭:接收时发生意外错误。
public static string PostWithResponse(string PostUrl, string DataUrl, string PostContentType, bool isTimeOut, int ResponseTimeOut)
{
string returnPostResponse = string.Empty;
HttpWebRequest PostRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
PostRequest.ContentType = PostContentType;
PostRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(DataUrl);
Stream os = null;
try
{
PostRequest.ContentLength = bytes.Length;
os = PostRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
if (isTimeOut)
{
PostRequest.Timeout = ResponseTimeOut;
}
}
catch (Exception ex)
{
//
}
finally
{
if (os != null)
{
os.Close();
}
}
try
{
HttpWebResponse PostResponse = (HttpWebResponse)PostRequest.GetResponse();
if (PostResponse == null)
{
//
}
StreamReader sr = new StreamReader(PostResponse.GetResponseStream());
returnPostResponse = sr.ReadToEnd().Trim();
}
catch (Exception ex)
{
//Error Found Here
}
return returnPostResponse;
}