Accessing Webservice by using Sys.Net.WebRequest

Anup1252000
Posted by in ASP.NET AJAX category on for Advance level | Views : 39612 red flag

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.


 Download source code for Accessing Webservice by using Sys.Net.WebRequest

Introduction
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.

Step 1:

Open Visual Studio > New Website> give the name as WebRequestApp> programming language as C#.

Step 2: 

Drag and drop the ScriptManager on to the page. And also drag and drop Html TextBox and a Button. And double click on to the button and it will generate the Buttonclick event within the javascript. 



Step 3: 

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.

Thanks
Page copy protected against web site content infringement by Copyscape

About the Author

Anup1252000
Full Name: anup hosur
Member Level:
Member Status: Member
Member Since: 8/1/2009 2:37:23 AM
Country: India



Login to vote for this post.

Comments or Responses

Posted by: Achilles on: 10/18/2009
anup thank you very much
that code and that explanation is superb. I have seen many code regarding sys.net class but your approach is definetely much clearer than mines. thanks again for sharing...

Posted by: Sandy060583 on: 1/19/2010
hi anup,
i have read the article & tried to implement but it gives me error

System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

So have you faced such error. how can i resolve this error..


Posted by: Rtpharry on: 2/1/2010
@Sandy060583: Server 500 error means that the server you are requesting data from has an error on it. Check that the website you are trying to retrieve information from is working correctly.

Login to post response

Comment using Facebook(Author doesn't get notification)