偶正在做抓取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,另外开贴结账。
多谢各位了。

解决方案 »

  1.   

    应该是webclient对namevaluecollection编码的问题,用下面的可以正确得到static void PostGetSite(string Url,string City){
    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","烟台");
      

  2.   

    帖子总是沉得这么快,顶。如果按照sina的form模式使用webclient就不能抓取到天气内容了么?如果可以namevaluecollection的编码可以如法炮制么?
      

  3.   

    建议把汉字先用Server.HtmlEncode转化下,偶现在做的是用XmlHttpRequest实现的。
      

  4.   

    http://www.host01.com/Get/Net/00020007/0561317413588586.htm
    不知道对你有没有用
      

  5.   

    好东东一定要顶一下.encoding在上学的时候没有怎么学习过现在发现真有用.
      

  6.   

    前不久,我也是在WAP应用程序中抓取sina的天气预报,但是没过一个月时间就不行了,后来发现,sina的天气预报POST 方法改掉了,不再用http://php.weather.sina.com.cn/search.php?city=""这种格式了,
      

  7.   

    用WebClient.DownloadData()简单一点:
    public byte[] PostGetSite(string Url,string City)
    {
      WebClient Return_WebC = new WebClient();
      return Return_WebC.DownloadData(Url + "?city=" + City);
    }
      

  8.   

    可以参考下CSDN小助手
    地址在下面。====CSDN 小助手 V2.5 ====
    CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
    速度快;使用方便;提供源代码。
    界面:http://blog.csdn.net/Qqwwee_Com/category/146601.aspx
    下载:http://szlawbook.com/csdnv2
      

  9.   

    新浪的天气查询可以用GET方式,比如:
    http://php.weather.sina.com.cn/search.php?city=广州
      

  10.   

    encoding 一律使用System.Text.Encoding.Default
    然后把Post的内容先用UrlEncode转换一下再发就没问题了.以前遇到过类似的问题.上面两个方法是关键.
      

  11.   

    直接分析
    http://weather.sina.com.cn/images/figureWeather/map/wholeNation.html的代码就可以了,很简单的信息提取我的www.blogweather.net上就是这样做的,其他地区的类似
      

  12.   

    借个地方说话,如果一定要用post方法发送gb2312的方式传递中文字符到表单中,应该使用什么样的方法呢?楼主所用的方法确实是在传递变量的时候少接受到了一个字符,烟台变成了台字,这是为什么呢?
      

  13.   

    下面的示例将文件从 http://www.contoso.com 下载到本地硬盘。
             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.);