谁见过这样的错误:基础连接已经关闭: 无法连接到远程服务器???

nxchen 2005-07-20 05:05:31
天气预报的代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;




namespace test
{


public class GetWeather : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblWeather;
protected System.Web.UI.WebControls.Label Weather2;
protected System.Web.UI.WebControls.Button btnGet2;
protected System.Web.UI.WebControls.Button btnGet;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnGet.Click += new System.EventHandler(this.btnGet_Click);
this.btnGet2.Click += new System.EventHandler(this.btnGet2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

public string weather_city(string city)
{
string temp = null;
try
{
string strURL = "http://weather.news.sina.com.cn/cgi-bin/figureWeather/search.cgi";
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method="POST"; //Post请求方式
request.ContentType="application/x-www-form-urlencoded"; //内容类型
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("city"); //参数经过URL编码
paraUrlCoded = paraUrlCoded + "=" + System.Web.HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding("GB2312"));
byte[] payload;
payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
request.ContentLength = payload.Length; //设置请求的ContentLength
Stream writer = request.GetRequestStream(); //获得请求流
writer.Write(payload,0,payload.Length); //将请求参数写入流
writer.Close(); //关闭请求流
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse(); //获得响应流
Stream s;
s = response.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
string HTML = "";
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}
HTML = HTML.Replace("<","<");
HTML = HTML.Replace(">",">");
int start,stop;
start = HTML.IndexOf("<img src=\"http://image2.sina.com.cn/dy/weather/images/figure/",0,HTML.Length);
stop = HTML.IndexOf("<td background=http://image2.sina.com.cn/dy/weather/images",start);
temp = HTML.Substring(start, stop - start);
}
catch (Exception x)
{
string a = x.Message.ToString();
}
return temp;
}


public static void getPage(String url)
{
try
{
WebRequest req = WebRequest.Create(url);
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\r\n已接收到响应流");
if (true)
{
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );

Console.WriteLine("HTML...\r\n");
while (count > 0)
{
String str = new String(read, 0, count);
Console.Write(str);
count = sr.Read(read, 0, 256);
}
Console.WriteLine("");
}
}
catch(Exception )
{
Console.WriteLine("\r\n找不到请求 URI,或者它的格式不正确");
}
}


private void btnGet_Click(object sender, System.EventArgs e)
{


System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

WebRequest wreq=WebRequest.Create("http://weather.yahoo.com/forecast/CHXX0037_c.html");

HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();

string HTML ="";
Stream s=wresp.GetResponseStream();

StreamReader objReader = new StreamReader(s);

string sLine = "";
int i = 0;

while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}

String temp= "";
int start,stop;

start = HTML.IndexOf("<!-- CURCON-->",0,HTML.Length);

stop = HTML.IndexOf("<!-- END CURCON-->",0,HTML.Length);

temp = HTML.Substring(start, stop - start);
start = temp.IndexOf("<b>");
stop = temp.IndexOf("</b>");

string degree = temp.Substring(start+3,stop - start -3);

start = temp.IndexOf("<img");
stop = temp.IndexOf("</td>",start);

string img = temp.Substring(start,stop - start);
lblWeather.Text = degree + "<br>" + img;



}

private void btnGet2_Click(object sender, System.EventArgs e)
{
WebRequest wreq=WebRequest.Create("http://cn.weather.yahoo.com/CHXX/CHXX0037/index_c.html");

HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();

string HTML ="";
Stream s=wresp.GetResponseStream();

StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));


string sLine = "";
int i = 0;

while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}

String temp= "";
int start,stop;

start = HTML.IndexOf("<table border=0 cellpadding=2 cellspacing=1 bgcolor=9999cc width=\"85%\">",0,HTML.Length);

stop = HTML.IndexOf("</table>",start)+8;


temp = HTML.Substring(start, stop - start);
Weather2.Text = temp;
}


public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy



{



public TrustAllCertificatePolicy()



{}







public bool CheckValidationResult(ServicePoint sp,



System.Security.Cryptography.X509Certificates.X509Certificate cert,



WebRequest req, int problem)



{



return true;



}



}


}
}







到了这两句就出错:
WebRequest wreq=WebRequest.Create("http://cn.weather.yahoo.com/CHXX/CHXX0037/index_c.html");

HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
说是 基础连接已经关闭: 无法连接到远程服务器,上面的网址是可以打开的,为什么连接会关闭?
查了下帮助,把帮助里面的代码copy下来运行也一样的错误,郁闷~~

...全文
260 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bj20082005 2005-11-01
  • 打赏
  • 举报
回复
mark
nxchen 2005-07-21
  • 打赏
  • 举报
回复
昨晚把代码拿回家运行了一下,发现居然没出现上面的错误,可能是被公司网络的防火墙挡住了,晕~
qsoo 2005-07-20
  • 打赏
  • 举报
回复
我写的获得新浪天气 你对比一下看看
string url="http://cgi.news.sina.com.cn/cgi-bin/figureWeather/search.cgi?city=%B9%E3%D6%DD";
WebRequest wreq=WebRequest.Create(url);
zedan 2005-07-20
  • 打赏
  • 举报
回复
太长了,没耐心,没能力。只能顶
jimu8130 2005-07-20
  • 打赏
  • 举报
回复
学习
ye_zi 2005-07-20
  • 打赏
  • 举报
回复
太长了,帮你顶一下

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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