You can use Web Service to Query IP addresses.

Source: Internet
Author: User

You can use Web Service to Query IP addresses.

Instance 01 implements a simple Web Service Access

This instance implements the IP address query interface service and returns the province, city, and region where the IP address is located based on the IP address input by the user. The instance will use the IP address library for information query, therefore, you can directly attach a database file to the CD resource file. The data import process is no longer described here.

The procedure is as follows:

(1) Open the Visual Studio 2017 development environment, click File> New> Project, and select "ASP. NET Web application option, and then change the project name and project path, as shown in Figure 12.1.

Figure 12.1 create an ASP. NET Web application

(2) Click "OK". The select project type dialog box is displayed. In this dialog box, select a project type such as Web Forms or MVC. Here, select the "Empty" Empty project type, click OK, as shown in Figure 12.2.

Figure 12.2 create an empty project

(3) the IP address query function is used to search for IP address segments. Therefore, you must convert the IP address to the Int type during filtering. This facilitates data range filtering, the following code defines the data type conversion method for IP addresses:

01 privatelong IPToNumber (string ip) // defines the IP to Int method. The ip address is the ip address to be queried by the user. 02 {03 try04 {05 char [] separator = newchar [] {'. '}; // defines the char-Type Split array 06 string [] items = ip. split (separator); // set the ip string to ". returns the array 07 // splits each segment of the IP address into a binary format, and then converts the binary value to an unsigned 32-bit integer 08 returnlong. parse (items [0]) <24 | long. parse (items [1]) <16 09 | long. parse (items [2]) <8 | long. parse (items [3]); 10} 11 catch12 {13 return 0; // if an exception occurs, return 014} 15}

(4) define the public method for obtaining IP addresses. This method will contain a string type parameter indicating the IP address of the region to be queried. The Code is as follows:

01 [WebMethod (Description = "")] 02 publicstringGetIPToArea (string ip) 03 {04 long IP = IPToNumber (ip); // input a string-type ip address, return the long type IP address 05 // create the Database connection object 06 SqlConnection conn = 07 newSqlConnection ("Server = 127.0.0.1; Database = School; Uid = sa; Pwd = 123456 "); 08 conn. open (); // Open Database 09 DataSet ds = newDataSet (); // define DataSet10 // create data adapter 11 SqlDataAdapter sda = 12 newSqlDataAdapter ("select * from IpAddress where IP1 <=" + IP + "and IP2> =" + IP, conn); 13 sda. fill (ds); // Fill DataSet14 conn. dispose (); // release the database to use the resource 15 DataTable dt = ds. tables [0]; // return the queried data table 16 if (dt. rows. count> 0) // determines if the total number of data exceeds 017 {18 string Province = (string) dt. rows [0] ["Province"]; // get Province 19 string City = (string) dt. rows [0] ["City"]; // obtain the City 20 return ip + "," + Province + "," + City; // splice the IP address with the province and city, and then return 21} 22 return "region information corresponding to the IP address not found"; // return no data information 23}

After the code is complete, the next step is to call the service. First, run the service page, which is the same as running the Web page. The displayed page contains a description of the Service Page, there are two clickable connections on the page. The connection corresponds to the method name defined in the service. One is that the GetIPToArea method was just defined and has the method description information, the other is the test method automatically generated when the service page is created. Click the GetIPToArea method connection here, as shown in Figure 12.3.

Figure 12.3 Web Service Help Page

After clicking this button, the page will jump to the page where you can call this method. This page helps us easily test the service created. There is a text box on the page used to enter the parameter value of the method, and there is a "call" button below, after the user enters the parameter value, click the "call" button to execute the service program and wait for the returned result, as shown in Figure 12.4.

Figure 12.4 Web service call page

The call result is displayed in a new window, as shown in Figure 12.5.

Figure 12.5 results page returned by the GetIPToArea Method

From the test results, we can see that the results returned by Web service methods are encoded in XML.

 

Instance 02 implements Web service access within the LAN

In a local environment, you can publish a Web service to IIS for caller access. This example shows how to call Web Services in a local environment.

(1) first open the IIS management tool and create a website under the "website" node in the IIS management tool. The directory specifies the prepared Web Service Application folder, name the website and assign the IP address and port number. This process is the same as creating a website. Click OK to complete the creation, as shown in Figure 12.6.

Figure 12.6 publish a Web service to IIS

Figure 12.6 The Web service site created in the specified directory is the Web service reference program created in instance 01. Therefore, create a website program to call the Web service, this process mainly demonstrates how to add "service reference.

(2) create a website and create Default. on the aspx page, add a TextBox Control, a Button control, and two Label controls to enter IP addresses, perform query operations, and display result information.

(3) Right-click the project and choose "add"> "service reference" from the shortcut menu. The "add service reference" dialog box is displayed, as shown in 12.7.

Figure 12.7 "add service reference" dialog box

(4) After the reference is added, a directory named App_WebReferences will be added to Solution Explorer, and the service with the namespace ServiceReference1 will be displayed in this directory, as shown in 12.8.

Figure 12.8 added ServiceReference1 Service

(5) In the Click Event of the "Query IP Address" button control on the Default. aspx page, call the GetIPToArea method of the service object to query information. The Code is as follows:

24 protectedvoid button#click (object sender, EventArgs e) 25 {26 ServiceReference1.WebService1SoapClient webService1SoapClient = 27 new Queue (); 28 string Result = webService1SoapClient. getIPToArea (this. textBox1.Text); 29 while (Result. indexOf (",")>-1) 30 {31 Result = Result. replace (",", "<br/>"); 32} 33 this. label1.Text = "The following are the output results of IP, province, and city:"; 34 this. label2.Text = Result; 35}

Run the Default. aspx page, enter a valid IP address in the text box, and click "Query IP Address". The page displays the query result, as shown in Figure 12.9.

Figure 12.9 Web service call results

This article is excerpted from "Zero infrastructure ASP. NET" published by tomorrow's technology. For more information, see the source !!!

 

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.