Go to DotNetFunda.com
 Online : 2120 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET AJAX > Accessing Webservice by using Sys.Net.WebRequest

  • Download the OOPS, ASP.NET and ADO.NET Training Videos for FREE, click here.

Submit Article | Articles Home | Search Articles |

Accessing Webservice by using Sys.Net.WebRequest

 Download source file
 Posted on: 8/2/2009 1:00:13 AM by Anup1252000 | Views: 6114 | Category: ASP.NET AJAX | Level: Advance | Print Article
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.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

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

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About anup hosur

Experience:2 year(s)
Home page:
Member since:Saturday, August 01, 2009
Level:Starter
Status: [Member]
Biography:
 Latest post(s) from Anup1252000

   ◘ 4 steps to enable instrumentation in WCF posted on 3/14/2010 4:02:05 AM
   ◘ Ajax Enabled WCF Service posted on 2/15/2010 1:10:10 AM
   ◘ Membership provider with WCF posted on 2/11/2010 7:00:47 AM
   ◘ Creating Multiple Channels in Channel factory in WCF posted on 2/11/2010 4:06:00 AM
   ◘ WCF Hosting in Windows Services posted on 1/28/2010 7:53:50 AM


 Responses
Posted by: Achilles | Posted on: 18 Oct 2009 05:33:17 AM

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 | Posted on: 19 Jan 2010 05:14:41 AM

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 | Posted on: 01 Feb 2010 04:52:33 AM

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

Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)