偶正在做抓取sina天气的代码,其中有一段是向http://php.weather.sina.com.cn/search.php提出post请求,并将查询的天气网页的结果取出来:
代码中有一段类是抓取天气的:
public byte[] PostGetSite(string Url,string City)
{
//Defined the value of the variants and object;
string Err;
byte[] Content;
string Post_City;
string tmp;
System.Text.ASCIIEncoding Return_Asc = new ASCIIEncoding();
System.Net.WebClient Return_WebC = new WebClient();
System.Collections.Specialized.NameValueCollection Post_Form_Value=new NameValueCollection();
//End the Defination
Post_City=City;
Post_Form_Value.Add("city",Post_City); //Give the value to the object or variant; Content=Return_WebC.UploadValues(Url,"POST",Post_Form_Value);
return Content;
}
调用的时候采用了:
private void button1_Click(object sender, System.EventArgs e)
{
CoolGG.GetInfo weather=new CoolGG.GetInfo();
byte[] str;
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
str=gb2312.GetBytes(this.City.Text);//this.City.Text="烟台"
string aa=gb2312.GetString(str).ToString();
MessageBox.Show(str.Length.ToString());
MessageBox.Show(utf8.GetString(str,0,str.Length));
this.Content.Text=gb2312.GetString(weather.PostGetSite(Url.Text,utf8.GetString(str,0,str.Length)));
}//
的方法,怎么还是不能得出天气类型,用什么方法才能将正确的城市名称post到sina去呢,这样的方法只是取得了一个"台"字。为什么不能得出“烟台”的天气结果呢?
一人能解出来给200,两个人解决出来每人100,另外开贴结账。
多谢各位了。
代码中有一段类是抓取天气的:
public byte[] PostGetSite(string Url,string City)
{
//Defined the value of the variants and object;
string Err;
byte[] Content;
string Post_City;
string tmp;
System.Text.ASCIIEncoding Return_Asc = new ASCIIEncoding();
System.Net.WebClient Return_WebC = new WebClient();
System.Collections.Specialized.NameValueCollection Post_Form_Value=new NameValueCollection();
//End the Defination
Post_City=City;
Post_Form_Value.Add("city",Post_City); //Give the value to the object or variant; Content=Return_WebC.UploadValues(Url,"POST",Post_Form_Value);
return Content;
}
调用的时候采用了:
private void button1_Click(object sender, System.EventArgs e)
{
CoolGG.GetInfo weather=new CoolGG.GetInfo();
byte[] str;
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
str=gb2312.GetBytes(this.City.Text);//this.City.Text="烟台"
string aa=gb2312.GetString(str).ToString();
MessageBox.Show(str.Length.ToString());
MessageBox.Show(utf8.GetString(str,0,str.Length));
this.Content.Text=gb2312.GetString(weather.PostGetSite(Url.Text,utf8.GetString(str,0,str.Length)));
}//
的方法,怎么还是不能得出天气类型,用什么方法才能将正确的城市名称post到sina去呢,这样的方法只是取得了一个"台"字。为什么不能得出“烟台”的天气结果呢?
一人能解出来给200,两个人解决出来每人100,另外开贴结账。
多谢各位了。
WebRequest req=WebRequest.Create(Url);
req.Method="POST";
req.ContentType="application/x-www-form-urlencoded";
byte[] by=Encoding.Default.GetBytes(String.Format("submit= 搜 索 &city={0}",City));
req.ContentLength=by.Length;
Stream str=req.GetRequestStream();
str.Write(by,0,by.Length);
str.Close();
WebResponse res=req.GetResponse();
StreamReader srd=new StreamReader(res.GetResponseStream(),Encoding.Default);
string s=srd.ReadToEnd();
srd.Close();
res.Close();
FileStream fs=new FileStream(@"g:\weather.htm",FileMode.Create,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs,Encoding.Default);
sw.Write(s);
sw.Flush();
sw.Close();
fs.Close();}
PostGetSite("http://php.weather.sina.com.cn/search.php","烟台");
不知道对你有没有用
public byte[] PostGetSite(string Url,string City)
{
WebClient Return_WebC = new WebClient();
return Return_WebC.DownloadData(Url + "?city=" + City);
}
地址在下面。====CSDN 小助手 V2.5 ====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
速度快;使用方便;提供源代码。
界面:http://blog.csdn.net/Qqwwee_Com/category/146601.aspx
下载:http://szlawbook.com/csdnv2
http://php.weather.sina.com.cn/search.php?city=广州
然后把Post的内容先用UrlEncode转换一下再发就没问题了.以前遇到过类似的问题.上面两个方法是关键.
http://weather.sina.com.cn/images/figureWeather/map/wholeNation.html的代码就可以了,很简单的信息提取我的www.blogweather.net上就是这样做的,其他地区的类似
string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath.);