Public methods of the Winform-webclient class

Source: Internet
Author: User

From the definition of MSDN: The WebClient class provides public methods for sending data to and receiving data from local, intranet, Internet resources identified to any URI.

We will recognize the usage of WebClient from a relatively long example. Program more than 100 lines, and No comments. But the follow-up will be added.

This example is done by invoking the APIs provided by Ip138 and Taobao to obtain an extranet IP and IP attribution method.

The first is the three functions used:

1 ///get the extranet IP address as the school is on the local area network. So it's more trouble .2 Private Static stringgetipaddress ()3 {4     stringTempip ="";5     Try6         {7WebRequest WR = WebRequest.Create ("http://www.ip138.com/ip2city.asp");8Stream s =WR. GetResponse (). GetResponseStream ();9StreamReader sr =NewStreamReader (S, encoding.default);Ten                 stringall = Sr. ReadToEnd ();//reading data from a Web site One  A                 intStart = all. IndexOf ("[") +1; -                 intEnd = All. IndexOf ("]", start); -Tempip = All. Substring (Start, End-start); the Sr. Close (); - s.close (); -     } -     Catch +     { -     } +     returnTempip; A}
1 ///obtaining IP attribution information based on the acquired extranet IP2  Public voidThreadProc (stringIP)3         {4             if(!isvaildipaddress (IP))5             {6MessageBox.Show ("illegal","Error", MessageBoxButtons.OK,7 messageboxicon.error);8             }9WebClient client =NewWebClient ();Ten             stringURI =@"http://ip.taobao.com/service/getIpInfo.php?ip="+IP; One             stringJsondata =Client. Downloadstring (URI);
The following statement requires a reference to Json.dll AIpcheckresult result = jsonconvert.deserializeobject<ipcheckresult> - (jsondata); - if(Result.code! =0) the { -MessageBox.Show ("Not Found"); - } -TextBox1.Text =result.data.Region; +TextBox2.Text =result.data.City; -TextBox3.Text = Result.data.Country +Result.data.ISP; +Textbox4.text = Result.data.Area +"Area"; A}
/// verifying IP with regular expressions Private BOOL Isvaildipaddress (string  IP)        {            string@ "^ (?:(? : 25[0-5]|2[0-4]\d| ((1\d{2}) | ([1-9]?\d))) \.) {3} (?: 25[0-5]|2[0-4]\d| ((1\d{2}) | ([1-9]?\d)))) $";             return regex.ismatch (IP, pattern);        }
Form_Load Event Invocation
1 privatevoid Form1_Load (object sender, EventArgs e)2 { 3 string IP = getipaddress (); 4 New Thread (() = ThreadProc (IP));
Always feel that the LAMDA expression is a good way for a thread to invoke a function that has parameters .... 5 thread. Start (); 6 }

Finally, two custom classes are used:

1 namespaceIPAddress2 {3     classIpaddressdata4     {5          Public stringCountry;//get the city where the IP resides6          Public stringarea;//get IP in your region7          Public stringregion;//get the province of your IP8          Public stringCity;//Get IP City9          Public stringISP;//Get IP-owned carriersTen          PublicIpaddressdata () One         { } A     } -}
1 namespace IPAddress 2 {3     class Ipcheckresult 4     {5public         int  code; 6          Public ipaddressdata data; 7     }8 }

Program results:

As for the program running up encountered from not creating the control of the thread access control, crossing Baidu a bit on the line. There are several workarounds. ('? ') )

Public methods of the Winform-webclient class

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.