Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
4.89/5 (5 votes)
See more:
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


C#
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
Updated 31-May-11 11:09am
v3
Comments
walterhevedeich 31-May-11 19:58pm    
Have you tried changing the encoding to UTF8?

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

C#
using (FooServiceClient proxy = new FooServiceClient())
{
  string result = proxy.GetMyName("sam");
}


Where FooServiceClient is the client generated.
 
Share this answer
 
v2
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.
 
Share this answer
 
v2
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[^]
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900