Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29918 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET AJAX > Accessing Webservice by using Sys.Net.WebRequest

Accessing Webservice by using Sys.Net.WebRequest

Article posted by Anup1252000 on 8/2/2009 | Views: 17804 | Category: ASP.NET AJAX | Level: Advance 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


 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

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

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About anup hosur

Experience:2 year(s)
Home page:
Member since:Saturday, August 01, 2009
Level:Starter
Status: [Member]
Biography:
 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.

>> Write Response - Respond to this post and get points
Related Posts

This article describes how to use ASP.NET AJAX PageMethods to submit data to the server and get response. It also shows how to serialize object into JSON format and access its properties in JavaScript.

This article explains a simple trick to set the page position to any coordinates after async Postback.

Using Validation Controls is faster than doing validation on the server side. Please note that there are some cases where you can’t set a rule. In that Case you will add those types of validation to your Business Logic Layer, when you validate in your Class Library before sending the Data to the Data Layer to be executed. There is only one reason why Microsoft created Validation controls. The Reason is simple “Never Trust the user input”

Windows applications are more stable than web application. Even if the web is not that stable like a windows application, there are lot advantages of building Web Applications and one thing that we remember as ex-Windows programmers is that the Forms did not Flicker in front of the eyes of the user. This irritates a lot of users. Microsoft came back with a good solution for this problem and they introduced Ajax. In this Article am going to show you how you can create Flicker Free pages.

This article describes the solution of the problem in which jQuery doesn't work for elements placed under ASP.NET AJAX UpdatePanel when partial postback is done.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:58:22 AM