.net模拟post本地没问题,远程System.Net.WebException基础连接已经关闭: 连接被意外关闭

lyrenlian 2015-11-06 04:29:19
准备写一个远程调用的方法,用的是.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;
}
...全文
395 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
秋的红果实 2015-11-06
  • 打赏
  • 举报
回复
真看不出有什么问题 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);改成 HttpWebRequest myRequest = HttpWebRequest.Create(url); 试一试
xdashewan 2015-11-06
  • 打赏
  • 举报
回复
防火墙啊,端口啊什么的导致超时的时候会发生,你可以检测下

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧