views:

178

answers:

1

Hi,

My requirement is that I will have SOAP data in XML.I will read from the file, and then send request to a webservice.Then, I need to write the response to a file. I am using vs 2005.

XmlDocument doc = new XmlDocument();
        doc.Load(@"path to xml file");
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.w3schools.com/webservices/tempconvert.asmx");


        req.Headers.Add("SOAPAction", "\"\"");

        req.ContentType = "text/xml;charset=\"utf-8\"";
        req.Accept = "text/xml";
        req.Method = "POST";
        Stream stm = req.GetRequestStream();
        doc.Save(stm);
        stm.Close();
        WebResponse resp = req.GetResponse();
        stm = resp.GetResponseStream();
        StreamReader r = new StreamReader(stm);
        Console.WriteLine(r.ReadToEnd());
A: 

You haven't really stated what problem you encountered. What were you expecting to happen and what actually happened?

Anyway, it appears that the web service you are trying to call was an ASP.Net web service (inferred from the .asmx extension on the URL). I suspect the problem was in your SOAPAction header which should be set to the web service namespace concatenated with the web method name. In your example, I think this would be http://tempuri.org/CelciusToFahrenheit. You also have the wrong XML namespace on the <CelciusToFahrenheit> element. It should be http://tempuri.org.

That said, this is not really a good way to consume a web service from C#. You should add the WSDL for the web service to your project as a web reference and then simply call through the generated class to invoke web service functionality.

As the service you are calling is an ASP.Net web service with documentation turned on, you can actually see the full request and response format at http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit

GBegen
why am I doing this way? Because my requirement is to call different webservices.So, the application should be able to dynamicaaly call a webservice given url,namespace,methodname and soapbody
jack
OK. Fair enough. You might want to, however, create a test web service client using a web reference and then capture the traffic when calling it with a tool like WireShark. You can then learn from the capture the required details, such as the correct SOAPAction header and XML namespace.
GBegen