How to make request to web service with soap message - asp.net /C#

If you have web services and want to call the web service from your c# code, you can try to use this code example. The code example uses HttpWebRequest to create a request for the web service. 

Code Behind

         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(<web service address>);  
request.Headers.Add("SOAPAction", "http://tempuri.org/" + <web service method>);
request.ContentType = "text/xml;charset=\"utf-8\"";
request.KeepAlive = false;
request.Timeout = 300000; // - in millisecond. (5 minit)
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
byte[] byteArray = Encoding.ASCII.GetBytes(<data you want to send to webservice>);
request.ContentLength = byteArray.Length;
Stream s = request.GetRequestStream();
s.Write(byteArray, 0, byteArray.Length);
s.Dispose();
s.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader resStream = new StreamReader(response.GetResponseStream());
result = resStream.ReadToEnd(); // read webservice response
dataToSend = "";
resStream.Dispose();
resStream.Close();
response.Close();
return result;


Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example