How to obtain the IP address area by calling webService in C #

Source: Internet
Author: User

Recently, during the development project, you need to obtain the IP address area used for statistical analysis. After some materials, I wrote a test program and quickly completed this function.

Now, let's share my implementation methods. If you have better implementations, please feel free to propose them.


First, I found a common WebService, http://webservice.webxml.com.cn/webservices/ipaddresssearchwebservice.asmx.

Call the getCountryCityByIp method to obtain the region address of the specific IP address. This WebService is described as follows:

GetCountryCityByIp


Enter an IP address to query country, city, owner, and other information. China if the country is not specified

Input parameter: IP address (automatically replaced ". "."), Return data: A one-dimensional String array String (1), String (0) = IP address; String (1) = query result or prompt

 

The next step is to obtain the IP address of the user's current computer. The specific implementation method is as follows:

/// <Summary>
/// Obtain the IP address of the Local Machine
/// </Summary>
/// <Returns> </returns>
Private string GetConnectNetAddress ()
{
String strUrl = "http://www.ip138.com/ip2city.asp"; // obtain the IP Address URL
Uri uri = new Uri (strUrl );
WebRequest webreq = WebRequest. Create (uri );
Stream s = webreq. GetResponse (). GetResponseStream ();
StreamReader sr = new StreamReader (s, Encoding. Default );
String all = sr. ReadToEnd (); // read the data format returned by the website: your IP address is [x. x]
Int I = all. IndexOf ("[") + 1;
String tempip = all. Substring (I, 15 );
String ip = tempip. Replace ("]", ""). Replace ("", ""). Replace ("<","");
Return ip;
}

This method is.

Of course, if the website has changed its domain name or does not exist that day, this method cannot be used.

The next thing is very simple. Add the preceding webservice to the project, and then call this webservice to obtain the Internet access area.

The specific implementation is as follows:

/// <Summary>
/// Region where the Internet IP address is obtained
/// </Summary>
/// <Returns> </returns>
Private string GetConnectNetAddressArea ()
{
String strIP = GetConnectNetAddress ();
IpAddressSearchWebService webService = new IpAddressSearchWebService ();
String [] strArea = webService. getCountryCityByIp (strIP );
Return strArea [1];
}

In this way, the region where the IP address is queried by the user is successfully obtained.


From weizhiai12's column
 

Related Article

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.