Example of IP address query using Web Service

Source: Internet
Author: User

Example of IP address query using Web Service

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:

Privatelong IPToNumber (string ip) // defines the IP to Int method. The ip address of the parameter is the ip address to be queried by the user {try {char [] separator = newchar [] {'. '}; // defines the char-Type Split array string [] items = ip. split (separator); // set the ip string to ". returns an array // splits each segment of the IP address into a binary format, and then converts the binary value to a 32-bit unsigned integer returnlong. parse (items [0]) <24 | long. parse (items [1]) <16 | long. parse (items [2]) <8 | long. parse (items [3]);} catch {return 0; // if an exception occurs, return 0 }}

(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:

[WebMethod (Description = "")] publicstringGetIPToArea (string ip) {long IP = IPToNumber (ip); // input a string-type ip address, return the IP address of the long TYPE // create the Database connection object SqlConnection conn = newSqlConnection ("Server = 127.0.0.1; Database = School; Uid = sa; Pwd = 123456"); conn. open (); // Open the database DataSet ds = newDataSet (); // define DataSet // create data adapter SqlDataAdapter sda = newSqlDataAdapter ("select * from IpAddress where IP1 <=" + IP + "and IP2> =" + IP, conn ); sda. fill (ds); // Fill DataSet conn. dispose (); // release the database to use the resource DataTable dt = ds. tables [0]; // return the queried data table if (dt. rows. count> 0) // determines if the total number of data exceeds 0 {string Province = (string) dt. rows [0] ["Province"]; // obtain the Province string City = (string) dt. rows [0] ["City"]; // obtain the City return ip + "," + Province + "," + City; // splice the IP address with the province and city, and return} return "region information corresponding to the IP address not found"; // return no data information}

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:

Protectedvoid button#click (object sender, EventArgs e) {ServiceReference1.WebService1SoapClient webService1SoapClient = new ServiceReference1.WebService1SoapClient (); string Result = webService1SoapClient. getIPToArea (this. textBox1.Text); while (Result. indexOf (",")>-1) {Result = Result. replace (",", "<br/>");} this. label1.Text = "The following is the output result of IP, province, and city:"; this. label2.Text = Result ;}

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.

The above example of implementing the IP address query function through Web Service is all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference and support for the customer's home.

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.