In this article we are going to see how to access the webservice from Sys.Net.WebRequest where we can find the country by entering the IP address.
Let us understand what is Sys.Net.WebRequest. WebRequest is the abstract
base class for the .NET Framework's request/response model for
accessing data from the Internet. An application that uses the
request/response model can request data from the Internet in a
protocol-agnostic manner, in which the application works with instances
of the WebRequest class while protocol-specific descendant classes carry out the details of the request.
Requests are sent from an application to a particular URI, such
as a Web page on a server. The URI determines the proper descendant
class to create from a list of WebRequest descendants registered for the application. WebRequest
descendants are typically registered to handle a specific protocol,
such as HTTP or FTP, but can be registered to handle a request to a
specific server or path on a server.
WebRequest Class throws an exception when error occurs. The Status is the property of WebExceptionStatus.
Lets start building our application.
Open Visual Studio > New Website> give the name as WebRequestApp> programming language as C#.
Next you need to call Sys.Net.WebRequest() in your button click event. Lets first look at the code.
In this set_url is used to set the url of the Sys.Net.WebRequest instance and we are setting the url to posthandler.aspx. httpverb that was used by Sys.Net.WebRequest class to issue the Web Request and the string that contains the HTTP verb for the Web request. value
must be an HTTP verb that is recognized by the Web
server, such as "GET" or "POST". We are passing the iptocheck as the parameter to the posthandler.aspx. set_body is used to set the body of Sys.Net.WebRequest. get_headers is used to get the header of the Sys.Net.WebRequest and we are appending it to the length of the body. and next we are invoking the method.Step 4:
Now lets add a new page called posthandler.aspx. In this lets drag and drop the literal control on to the page.
Now in the Page_Load, add these lines and will explain you.
In this approach we are using the external webservice which exist in this Website.
In this we are going to copy the getmethod and paste in the string.Format.. and we are using Server.HtmlDecode because my default it will return the result in xml format so as to to decode the xml format we use htmldecode method.Step 5:
Now its a time to run the application.
I hope you people like this application.