.net模拟post本地没问题,远程System.Net.WebException基础连接已经关闭: 连接被意外关闭
准备写一个远程调用的方法,用的是.net的模拟post,在本地测试无误后放到服务器,就报错了,具体是
捕捉到 System.Net.WebException
HResult=-2146233079
Message=基础连接已经关闭: 连接被意外关闭。
Source=System
StackTrace:
在 System.Net.HttpWebRequest.GetResponse()
在 _Default.SendServices(String method, String para) 位置 Default.aspx.cs:行号 74
InnerException:
查了很久,找不出原因。
这是具体调用方法
public static string SendServices(string method, string para)
{
string url = ConfigurationManager.AppSettings["RegisterUrl"] + "AjaxHandler/InterfaceHandler.ashx/" + method;
string contenttype = "application/x-www-form-urlencoded";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = contenttype;
myRequest.ContentLength = para.Length;
try
{
Stream newStream = myRequest.GetRequestStream();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postdata = encoding.GetBytes(para);
newStream.Write(postdata, 0, para.Length);
newStream.Close();
}
catch (WebException et)
{
}
HttpWebResponse res;
string content;
try
{
res = (HttpWebResponse)myRequest.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
content = sr.ReadToEnd();//得到结果
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
content=ex.ToString();
}
return content;
}