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...