Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soap Request in C#

I've got a soap request I've written in http & javascript but I cannot seem to convert it into C# correctly.

Original: (Works)

<button onclick="doStuff()" type="submit">Send</button>

<textarea name="REQUEST_DATA" cols=120 rows=17 >
<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<createModifyRequest>
<username>josephs</username>
<lookupIds>
<lookupIds>4225</lookupIds><!--firepass-->
</lookupIds>
</createModifyRequest>
</soap:Body>
</soap:Envelope>
</textarea>

<script language="javascript"> 

function doStuff() {

var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");

xmlhttp.open("POST", "http://tpdev-itreq.transpower.co.nz:7777/usr/services/CreateModifyRequest", false);
xmlhttp.setRequestHeader("SOAPAction", "createModifyRequest");

var userpass = "josephs" + ":" + "pass";
xmlhttp.setRequestHeader("Authorization", "Basic " + (userpass));

xmlhttp.setRequestHeader("Content-Type", "text/xml");
xmlhttp.send(REQUEST_DATA.value);

}

Converted in C# (Does not work)

private void button1_Click(object sender, EventArgs e)
{
    string soap =@"<?xml version=""1.0"" encoding=""utf-8""?>
    <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
      xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
   xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
  <soap:Body>
    <createModifyRequest>
        <username>josephs</username>
        <lookupIds>
            <lookupIds>4225</lookupIds>
            <!--firepass-->
       </lookupIds>
    </createModifyRequest>
  </soap:Body>
</soap:Envelope>";

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://tpdev-itreq.transpower.co.nz:7777/usr/services/CreateModifyRequest");
    req.Headers.Add("SOAPAction", "\"createModifyRequest\"");

    var userpass = "josephs" + ":" + "pass";

    req.Headers.Add("Authorization", "Basic " + (userpass));
    // req.Headers.Add("Content-Type", "text/xml");

    req.ContentType = "text/xml;charset=\"utf-8\"";
    req.Accept = "text/xml";
    req.Method = "POST";

    using (Stream stm = req.GetRequestStream())
    {
        using (StreamWriter stmw = new StreamWriter(stm))
        {
            stmw.Write(soap);
        }
    }

    WebResponse response = req.GetResponse();

    Stream responseStream = response.GetResponseStream();
    // TODO: Do whatever you need with the response
}

At the moment when I run the C# code I get an internal 500 server error, so what have I done wrong?

like image 833
stefan Avatar asked May 30 '13 22:05

stefan


People also ask

What is a SOAP request?

What is SOAP. SOAP is the Simple Object Access Protocol, a messaging standard defined by the World Wide Web Consortium and its member editors. SOAP uses an XML data format to declare its request and response messages, relying on XML Schema and other technologies to enforce the structure of its payloads.

Does SOAP have GET request?

I always used POST but according to the W3C standard, SOAP supports both POST and GET methods. Edit: After some research, it seems that it's not completely true, as you can see here. It is theoretically possible to use GET because POST and GET are methods of HTTP transport protocol and SOAP can be used over HTTP.

Is SOAP request GET or POST?

SOAP also defines a binding to the HTTP protocol. When binding to HTTP, all SOAP requests are sent through HTTP POST.


2 Answers

I have tried to reproduce your problem. Currently i am not able to create your request but i have generated local request with your data. One thing I came to know is if I remove double quotes(") around the utf-8 and it worked fine. Just pass charset=utf-8 instead of charset=\"utf-8\""

I am not sure it will work for you or not.

like image 120
Finisher001 Avatar answered Oct 17 '22 21:10

Finisher001


Is there a reason you can't just use Visual Studio's built in support for SOAP web services?

You can add a service reference or a web reference (depending on whch framework version you are targeting).

Then you can just use the proxy class that VS creates for you.

There's no advantage to writing all of the HTTP code yourself. Actually, there's a big disadvantage, in that you are aren't getting proper data types from the WSDL of your SOAP service.

like image 25
Matt Johnson-Pint Avatar answered Oct 17 '22 23:10

Matt Johnson-Pint