Click here to Skip to main content
Click here to Skip to main content

Calling a webservice programmatically

, 30 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Calling webservice at runtime


Calling a webservice is basically easy in .NET, it is only a matter of adding the webservice as a web reference and using the generated classes. Sometimes you will need to build requests and send them programmatically. In this article we will see you to do it.


Intermediate knowledge in .NET, webservices.

Using the code

string completeUrl ="<a href="http://localhost/testwebservice">http://localhost/testwebservice</a>";

// Create a request for the URL.         
WebRequest request = WebRequest.Create(completeUrl);

// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;

// If you have a proxy configured.
WebProxy proxyObject = new WebProxy("<a href=""></a>", true);
request.Proxy = proxyObject;

//Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();

// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();

// Cleanup the streams and the response.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Wael Al Wirr
Program Manager INVESTBANK
Jordan Jordan
Wael Al Wirr’s Specialties:
•Microsoft BizTalk Server 2004, 2006, 2010
•ASP.Net, C#, ADO.Net and .Net Framework (1.1, 2.0, 3.0, 3.5, 4.0)
•Windows Communication Foundation
•Windows Workflow Foundation
•Windows AppFabric
•ADO.NET Entity Framework
•Microsoft Line Of Business Adapter Framework
•SQL Server 2000, 2005
•Web development
•Database design
•Web Services
•Build, deployment script
•Microsoft Source Safe, Microsoft Team Foundation Server
Follow on   Twitter

Comments and Discussions

QuestionHow can we call the web service's different methods in your example? Pinmemberhiteshprajpati25-Mar-13 1:00 
GeneralMy vote of 1 [modified] PinmemberSelvin31-May-12 1:43 
sorry, but you not mentioned as an example of webservice should look and how to eventualy post some parameters.
So this tip is only about how to do webrequest to http server not how to call webservice with webrequest ...

modified 31-May-12 7:13am.

GeneralRe: My vote of 1 PinmemberWael Al Wirr31-May-12 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 30 May 2012
Article Copyright 2012 by Wael Al Wirr
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid