关于WebRequest问题(很菜的问题,但是找不到头绪),谢谢帮忙.
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create("http://gather.sohu.com/life/forecast/query.asp?cityname=北京")
本身我得到应当是关于成都的信息,可是
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
StreamReader sr = new StreamReader(oResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
sResultContents = sr.ReadToEnd();
oResponse.Close();
得到的确实不是关于成都的信息,但在浏览器中输入"http://gather.sohu.com/life/forecast/query.asp?cityname=北京"之后却是正确的,请问这是为什么?
一开始我想的是编码问题,用了“http://gather.sohu.com/life/forecast/query.asp?cityname=“+HttpUtility.UrlEncode("北京")”可还是不行,得到的代码是“暂时没有您要的城市信息!“也就是“北京“不起作用。为什么啊。谢谢了。
帮忙试试啊。大恩大的。
问题点数:20、回复次数:3Top
1 楼panyee(快乐王子)回复于 2003-08-03 23:53:07 得分 20
string strUrl = "http://gather.sohu.com/life/forecast/query.asp?cityname=";
strUrl = strUrl + System.Web.HttpUtility.UrlEncode("北京", System.Text.Encoding.GetEncoding("gb2312"));
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(strUrl);
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
StreamReader sr = new StreamReader(oResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
string sResultContents = sr.ReadToEnd();
oResponse.Close();
byte[] bytes = System.Text.Encoding.GetEncoding("gb2312").GetBytes(sResultContents);
FileStream fs = new FileStream("c:\\2.htm", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();
MessageBox.Show("OK");Top
2 楼menox(大傻)回复于 2003-08-04 11:24:34 得分 0
厉害太感谢了。Top
3 楼menox(大傻)回复于 2003-08-04 11:30:48 得分 0
能不能告诉我是为什么啊,好像就是你的写法上不一样。
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create("http://gather.sohu.com/life/forecast/query.asp?cityname="+HttpUtility.UrlEncode("北京"))
和你的
strUrl = strUrl + System.Web.HttpUtility.UrlEncode("北京", System.Text.Encoding.GetEncoding("gb2312"));
Top




