Click here to Skip to main content
13,248,295 members (36,817 online)
Click here to Skip to main content
Add your own
alternative version

Stats

63.2K views
7 bookmarked
Posted 30 May 2012

Calling a webservice programmatically

, 30 May 2012
Rate this:
Please Sign up or sign in to vote.
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

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)

Share

About the Author

Wael Al Wirr
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.

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 1:00
memberhiteshprajpati25-Mar-13 1:00 
GeneralMy vote of 1 Pin
Selvin31-May-12 1:43
memberSelvin31-May-12 1:43 
GeneralRe: My vote of 1 Pin
Wael Al Wirr31-May-12 1:58
memberWael Al Wirr31-May-12 1: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
Web01 | 2.8.171114.1 | Last Updated 30 May 2012
Article Copyright 2012 by Wael Al Wirr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid