Click here to Skip to main content
12,955,170 members (60,126 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 30 May 2012

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

You may also be interested in...

Comments and Discussions

QuestionHow can we call the web service's different methods in your example? Pin
hiteshprajpati25-Mar-13 0:00
memberhiteshprajpati25-Mar-13 0:00 
GeneralMy vote of 1 Pin
Selvin31-May-12 0:43
memberSelvin31-May-12 0:43 
GeneralRe: My vote of 1 Pin
Wael Al Wirr31-May-12 0:58
memberWael Al Wirr31-May-12 0:58 

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

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

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