Click here to Skip to main content
11,429,687 members (68,585 online)
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

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 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
•HTML, XML, XSD, XSLT
•JavaScript
•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? Pin
hiteshprajpati25-Mar-13 1:00
memberhiteshprajpati25-Mar-13 1:00 
GeneralMy vote of 1 [modified] 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    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
Web04 | 2.8.150428.2 | Last Updated 30 May 2012
Article Copyright 2012 by Wael Al Wirr
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid