(代理问题)为什么在调用web service 时 远程主机易做图关闭了一个现有的连接。
报错:System.Net.Sockets.SocketException: 远程主机易做图关闭了一个现有的连接。我这个web wervice 是和这个页面的代码方式相同基本相同,,,
我把程序放到一台有网的电脑上,在另外一台没有网的电脑易做图问程序,就出现这个问题了...
就是执行到这句就报错了。HttpWebResponse webrs = (HttpWebResponse)webrt.GetResponse();
谢谢大家了,帮忙看看呢
[WebMethod]
public string GetWeather(string url)
{
// string uurl2 = "http://www.sina.com.cn/iframe/2006/weather/610101.html";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
HttpWebResponse response = null;
try
{
try
{
response = (HttpWebResponse)request.GetResponse(); //在这里可能出错,因为没有cookie
}
catch (WebException exp)
{
if (exp.Status == WebExceptionStatus.ProtocolError)
{
HttpWebRequest request_ = (HttpWebRequest)WebRequest.Create(url); //通过访问网站获取一个cookie
using (HttpWebResponse response_ = (HttpWebResponse)request_.GetResponse())
{
string str = response_.Headers["Set-Cookie"];
string url_cookie = str.Split(';')[0]; //得到cookie
request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.Cookie, url_cookie); //再次Create原始搜索地址,并加入cookie
response = (HttpWebResponse)request.GetResponse(); //就是这里报错
}
}
}
Stream mystream = response.GetResponseStream();
// System.Text.Encoding myencoding = System.Text.Encoding.GetEncoding("utf-8");
System.Text.Encoding myencoding = System.Text.Encoding.GetEncoding("gb2312");
System.IO.StreamReader mystreamreader = new System.IO.StreamReader(mystream, myencoding);
string strHtmlContent = mystreamreader.ReadToEnd();
//Response.Write(strHtmlContent);
mystream.Close();
mystreamreader.Close();
response.Close();
return strHtmlContent;
}
catch(Exception e)
{
return e.Message;
}
}
非常感谢了 --------------------编程问答-------------------- 参考
补充:.NET技术 , ASP.NET