Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SOAP
HI i have a web service and i added the url as web refrence to other website from where i need to send request, i am looking over google but it is kind of hard for me as i was new to this , what i am trying to do here is on a button click event i have to invoke the store procedure with parameters from textboxes and the result should be in xml format , i am having trouble on some parts of code like passing parametrs and stuff can some help me out on how to acieve it , the below is the code i am working on , please let me know if its is having any errors, thanks in advance
 

private static string webservicecall(string getbrokerdetails)
    {
 
        WebRequest req = WebRequest.Create("http://localhost/SOAPREQUESTRESPONSE/Service.asmx");
        HttpWebRequest httpreq = (HttpWebRequest)req;
        httpreq.Method = "POST";
        httpreq.ContentType = "text/xml; charset=utf-8";
        httpreq.Headers.Add("SOAPAction:http://tempuri.org/getbrokerdetails");
        httpreq.ProtocolVersion=HttpVersion.Version11;
        httpreq.Credentials=CredentialCache.DefaultCredentials;
        Stream  str=httpreq.GetRequestStream();
        StreamWriter strwriter=new StreamWriter(str,Encoding.ASCII);
        StringBuilder soaprequest=new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        soaprequest.Append ("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
        soaprequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
        soaprequest.Append("<GetMyName xmlns=\"http://tempuri.org/\"><name>Sam</name></GetMyName>");
        soaprequest.Append("<compcode>blgf</compcode>");
        soaprequest.Append("<period>20111st</period>");
        soaprequest.Append("</soap:Body></soap:Envelope>");
 
       strwriter.Write(soaprequest.ToString());
       strwriter.Close();
        HttpWebResponse res=(HttpWebResponse)httpreq.GetResponse();
        StreamReader rdr=new StreamReader(res.GetResponseStream());
        string result=rdr.ReadToEnd();
        return result;
 

 
    }
Posted 31-May-11 11:11am
Edited 31-May-11 12:09pm
Keith Barrow149.2K
v3
Comments
walterhevedeich at 31-May-11 19:58pm
   
Have you tried changing the encoding to UTF8?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It is not usual (and hardish!) to hand-roll your own reqest like this, normally you generate a proxy client via a tool.
 
You can probably (definately if you have the webservice project available to you) right click the client project and add a Web Reference, then point the wizard at the service you want to consume, this generated the client.
 
Failing that you can run a tool called wsdl.exe against the service, assuming it has a wsdl exposed. There is some help here[^].
 
Once you have the client calling the web method is as simple as creating an instance of the generated proxy client, calling the method on the instance then closing the proxy.
 
This is the best article I could find :consuming data web asmx service[^], you just have to ignore the WPF stuff. The code for your example woul look like
 
using (FooServiceClient proxy = new FooServiceClient())
{
  string result = proxy.GetMyName("sam");
}
 
Where FooServiceClient is the client generated.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

if you have wsdls given from a 3rd party you can generate class objects and functions from them and make web services calls... (as answered by keith)
 
though I sometimes use SoapUI tool to test my soap request. once successful i copy that request and save it as xml file. then i read that xml file and put values in tags where required and post it as http web request request. you can also sign it using certificate if required.. it works.
however i know the approach above is not a good one and can be said as a heck.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think something wrong with your header :
 
 httpreq.Headers.Add("SOAPAction:http://tempuri.org/getbrokerdetails");
 
As per my understanding, you are not providing correct URL Action name because your service method pointing Localhost so try this:
 
httpreq.webRequest.Headers.Add("SOAPAction", "http://localhost/SOAPREQUESTRESPONSE/WebMethodName");
 
Make sure request format is correct so debug the code and post here request string if it not working.
 
Ref. URL:
 
http://mikehadlow.blogspot.com/2006/05/making-raw-web-service-calls-with.html[^]
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100