Common Weather Forecast

Source: Internet
Author: User
Using system. net;

Public static string getwerther (string cityname)
{
WebClient mywea = new WebClient ();
Mywea. Credentials = credentialcache. defaultcredentials;
String strurl = "http://weather.tq121.com.cn/mapanel/index_new.php? City = "+ httputility. urlencode (cityname, encoding. Default) +"; // URL of the webpage to be obtained
// Response. Write (strurl );
Uri myuri = new uri (strurl );
//
// Mywea. querystring. Add ("city", cityname );
String OK, okone, oktwos, okthrees, okfours;
Int oktwo, okthree, okfour;
/** // * WebClient myweather = new WebClient ();
Myweather. Credentials = credentialcache. defaultcredentials ;*/
Try
{

// OK = mywea. responsebody;
Byte [] pagedata = mywea. downloaddata (myuri );
// Download data from the resource and return a byte array. (Add @ because there is a "/" symbol in the middle of the URL)

// You only need to use one of the following two sentences at a time. The function is the same as that used to convert character sets.
// If you use gb2312 to retrieve the website page, use this sentence.
OK = encoding. Default. getstring (pagedata );
// Use this sentence if the UTF-8 is used to get the website page
// String OK = encoding. utf8.getstring (pagedata );
If (OK. indexof ("<title> weather search </title>")> 0 & OK. indexof (".tq121.com.cn")> 0)
{
Okone = OK. substring (OK. indexof ("<SPAN class = \" big-CN \ ">") + 22, 8 );
If (okone = "city not found ")
{
OK = "Weather Forecast Information \ n" for "+ cityname +" not found ";
}
Else
{
Oktwo = OK. indexof ("<TD width = \" 160 \ "align = \" center \ "valign = \" Top \ "class = \" Weather \ "> ");
// Alert ();
Oktwos = OK. substring (oktwo + 60, OK. indexof ("</TD>", oktwo + 60)-oktwo-60 );
Okthree = OK. indexof ("<TD width = \" 160 \ "align = \" center \ "valign = \" Top \ "class = \" weatheren \ "> ", oktwo + 60 );
Okthrees = OK. substring (okthree + 62, OK. indexof ("</TD>", okthree + 60)-okthree-62 );
Okfour = OK. indexof ("<TD width = \" 153 \ "valign = \" Top \ "> <SPAN class = \" big-CN \ ">", okthree + 60 );
Okfours = OK. substring (okfour + 50, OK. indexof ("<br>", okfour)-okfour-50 );
OK = "& nbsp;" + cityname + ":" + oktwos + "& nbsp;" + okthrees + "& nbsp;" + okfours;
}
}
}
Catch
{
OK = "no weather forecast for cities now" + cityname + ";
}
OK = "<span style = \" color: # ff0000; \ ">" + OK + "</span> ";
Return OK;
}

Of course, it is actually the prototype of a thief program. I locally use UTF-8 encoding, the object page is gb2312, so in the page suffix processing cannot use server. urlencode, but can only use httputility. urlencode for text encoding. If the local version is gb2312, encoding is not required, but I did not test it. I just want to provide you with an idea ..

As for the following, all of them are only useful content for extracting string obtained through analysis. There are many similar code on the Internet. You can make a comprehensive comparison between them, and they are all applications of WebClient...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.