问题描述
准备写一个远程调用的方法,用的是.net的模拟post,在本地测试无误后放到服务器,就报错了,具体是捕捉到System.Net.WebExceptionHResult=-2146233079Message=基础连接已经关闭:连接被意外关闭。Source=SystemStackTrace:在System.Net.HttpWebRequest.GetResponse()在_Default.SendServices(Stringmethod,Stringpara)位置Default.aspx.cs:行号74InnerException:查了很久,找不出原因。这是具体调用方法publicstaticstringSendServices(stringmethod,stringpara){stringurl=ConfigurationManager.AppSettings["RegisterUrl"]+"AjaxHandler/InterfaceHandler.ashx/"+method;stringcontenttype="application/x-www-form-urlencoded";HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(url);myRequest.Method="POST";myRequest.ContentType=contenttype;myRequest.ContentLength=para.Length;try{StreamnewStream=myRequest.GetRequestStream();ASCIIEncodingencoding=newASCIIEncoding();byte[]postdata=encoding.GetBytes(para);newStream.Write(postdata,0,para.Length);newStream.Close();}catch(WebExceptionet){}HttpWebResponseres;stringcontent;try{res=(HttpWebResponse)myRequest.GetResponse();StreamReadersr=newStreamReader(res.GetResponseStream(),Encoding.UTF8);content=sr.ReadToEnd();//得到结果}catch(WebExceptionex){res=(HttpWebResponse)ex.Response;content=ex.ToString();}returncontent;}
解决方案
本帖最后由 lyrenlian 于 2015-11-06 16:43:36 编辑
解决方案二:
防火墙啊,端口啊什么的导致超时的时候会发生,你可以检测下
解决方案三:
真看不出有什么问题HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(url);改成HttpWebRequestmyRequest=HttpWebRequest.Create(url);试一试