Click here to Skip to main content
15,881,812 members
Articles / Web Development / ASP.NET
Tip/Trick

Calling a webservice programmatically

Rate me:
Please Sign up or sign in to vote.
3.22/5 (7 votes)
30 May 2012CPOL 83.8K   7   3
Calling webservice at runtime

Introduction

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.

Background

Intermediate knowledge in .NET, webservices.

Using the code

C++
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="http://proxy.com/">http://proxy.com/</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.

reader.Close();
dataStream.Close();            
response.Close();

License

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


Written By
Program Manager
Jordan Jordan
Self-motivated, creative and results-driven technology executive who is well versed and experienced in leveraging an end-to-end, holistic vision of business objectives to drive full technology / business alignment.

Skilled in grasping business needs and sudden market demand’s shifts by diving into latest business / technology trends, selecting the best fit business model / technology to create a positive reflection on revenue. His multifaceted approach has enabled him to deliver key solutions across a wide range of disciplines including design, development, UX / UI, Business Intelligence.

Technical Specialties are in .Net, Java, Spring Boot, Maven, MS SQL, Oracle, Postgesql, Redis, Javascript, Bootstrap, Angular 2.

Comments and Discussions

 
QuestionHow can we call the web service's different methods in your example? Pin
hiteshprajpati25-Mar-13 0:00
hiteshprajpati25-Mar-13 0:00 
GeneralMy vote of 1 Pin
Selvin31-May-12 0:43
Selvin31-May-12 0: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 Pin
Wael Al Wirr31-May-12 0:58
Wael 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.