Click here to Skip to main content
13,048,584 members (91,178 online)
Rate this:
Please Sign up or sign in to vote.
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

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";
        Stream  str=httpreq.GetRequestStream();
        StreamWriter strwriter=new StreamWriter(str,Encoding.ASCII);
        StringBuilder soaprequest=new StringBuilder("<soap:Envelope xmlns:xsi=\"\"");
        soaprequest.Append ("xmlns:xsd=\"\" ");
        soaprequest.Append("<GetMyName xmlns=\"\"><name>Sam</name></GetMyName>");
        HttpWebResponse res=(HttpWebResponse)httpreq.GetResponse();
        StreamReader rdr=new StreamReader(res.GetResponseStream());
        string result=rdr.ReadToEnd();
        return result;

Posted 31-May-11 10:11am
Updated 31-May-11 11:09am
Keith Barrow158.9K
walterhevedeich 31-May-11 19:58pm
Have you tried changing the encoding to UTF8?
Rate this: bad
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.
Rate this: bad
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I think something wrong with your header :


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:[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Jun 2011
Copyright © CodeProject, 1999-2017
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