发送Http请求时报错,请各位大仙们多帮忙,谢谢!
{"请求被中止: 请求已被取消。"}
[System.Net.WebException]: {"请求被中止: 请求已被取消。"}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
InnerException: {"在写入所有字节之前不能关闭流。"}
Message: "请求被中止: 请求已被取消。"
Source: "System"
StackTrace: " 在 System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)\r\n 在 System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)\r\n 在 System.Net.ConnectStream.Dispose(Boolean disposing)\r\n 在 System.IO.Stream.Close()\r\n 在 System.IO.StreamWriter.Dispose(Boolean disposing)\r\n 在 System.IO.StreamWriter.Close()\r\n 在 HttpRequestAndReponse.SendRequest(String indata) 位置 c:\\Inetpub\\wwwroot\\QPayment\\App_Code\\Common\\HttpRequestAndReponse.cs:行号 45"
TargetSite: {Void CloseInternal(Boolean, Boolean)}
代码:
protected static HttpWebRequest SendRequest(string indata)
{
HttpWebRequest req;
try
{
//发起HTTP请求
req = (HttpWebRequest)WebRequest.Create(Constant.HTTP_URL);
req.Method = Constant.HTTP_METHOD;
req.ContentType = Constant.HTTP_CONTENTTYPE;
req.ContentLength = indata.Length;
req.UserAgent = Constant.HTTP_USERAGENT;
req.KeepAlive = true;
req.Headers.Add("Cmbc-E2bTxcode", "yq0100");
req.Headers.Add("Cmbc-E2bEcode", "rtdl");
Stream requestStream = req.GetRequestStream();
StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.GetEncoding("GB2312"));
streamWriter.Write(indata);
req.Timeout = 30000;
streamWriter.Close();
requestStream.Close();
return req;
}
catch(Exception err)
{
throw new System.Exception("0");// 0:发送请求失败
}
}