I have a webservice that claims the following:
SOAP 1.1
The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
Now I tried to produce this using HttpWebRequest and sent the following(pulle d from a packet watcher):
For return result:
If I use a regular old POST urlencoded style, i am able to execute the function fine, but trying to do soap fails saying the contenttype is wrong?
Any ideas?
Edit: The (correct version) code I use to produce this:
[code=c#]private static bool DoSoap11Style()
{
bool retval = false;
try
{
string test = "http://localhost/testdb/UnitReporting.a smx/UpdateInformati on";
HttpWebRequest hwr = (HttpWebRequest )HttpWebRequest .Create(test);
hwr.Method = "POST";
hwr.ContentType = "text/xml; charset=utf-8";
hwr.Headers.Add ("SOAPAction ", "http://mylocation/UpdateInformati on");
string postData = GetPostDataSoap 11();
byte[] byte1 = Encoding.UTF8.G etBytes(postDat a.ToString());
hwr.ContentLeng th = byte1.Length;
Stream newStream = hwr.GetRequestS tream();
newStream.Write (byte1, 0, byte1.Length);
newStream.Close ();
HttpWebResponse hresp = (HttpWebRespons e)hwr.GetRespon se();
}
catch (Exception ee)
{
bool ignoreme = (ee.Message == "");
retval = false;
}
return retval;
}
private static string GetPostDataSoap 11()
{
StringBuilder postData = new StringBuilder(" ");// "firstone=" + "";
postData.Append ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
postData.Append ("<soap:Envelop e xmlns:xsi=\"htt p://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"htt p://www.w3.org/2001/XMLSchema\" xmlns:soap=\"ht tp://schemas.xmlsoap .org/soap/envelope/\">\r\n");
postData.Append (" <soap:Body>\r\n ");
postData.Append (" <UpdateInformat ion xmlns=\"http://mylocation/\">\r\n");
postData.Append Format(null, " <WhenSent>{0} </WhenSent>\r\n", DateTime.Now);
postData.Append Format(null, " <StatusMessage> {0}</StatusMessage>\ r\n", "Nothing to report");
postData.Append (" </UpdateInformati on>\r\n");
postData.Append (" </soap:Body>\r\n" );
postData.Append ("</soap:Envelope>\ r\n");
return postData.ToStri ng();
}[/code]
SOAP 1.1
The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
Code:
POST /testdb/UnitReporting.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: [B]length[/B]
SOAPAction: "http://mylocation/UpdateInformation"
<?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>
<UpdateInformation xmlns="http://mylocation/">
<WhenSent>[B]dateTime[/B]</WhenSent>
<StatusMessage>[B]string[/B]</StatusMessage>
</UpdateInformation>
</soap:Body>
</soap:Envelope>
Code:
POST /testdb/UnitReporting.asmx/UpdateInformation HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: http://mylocation/UpdateInformation
Content-Length: 450
Host: localhost
<?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>
<UpdateInformation xmlns="http://mylocation/">
<WhenSent>12/30/2008 3:28:01 PM</WhenSent>
<StatusMessage>Nothing to report</StatusMessage>
</UpdateInformation>
</soap:Body>
</soap:Envelope>
Code:
HTTP/1.1 500 Internal Server Error Server: Microsoft-IIS/5.1 Date: Tue, 30 Dec 2008 20:28:02 GMT X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 Cache-Control: private Content-Type: text/plain; charset=utf-8 Content-Length: 236 System.InvalidOperationException: Request format is invalid: text/xml; charset=utf-8. at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
If I use a regular old POST urlencoded style, i am able to execute the function fine, but trying to do soap fails saying the contenttype is wrong?
Any ideas?
Edit: The (correct version) code I use to produce this:
[code=c#]private static bool DoSoap11Style()
{
bool retval = false;
try
{
string test = "http://localhost/testdb/UnitReporting.a smx/UpdateInformati on";
HttpWebRequest hwr = (HttpWebRequest )HttpWebRequest .Create(test);
hwr.Method = "POST";
hwr.ContentType = "text/xml; charset=utf-8";
hwr.Headers.Add ("SOAPAction ", "http://mylocation/UpdateInformati on");
string postData = GetPostDataSoap 11();
byte[] byte1 = Encoding.UTF8.G etBytes(postDat a.ToString());
hwr.ContentLeng th = byte1.Length;
Stream newStream = hwr.GetRequestS tream();
newStream.Write (byte1, 0, byte1.Length);
newStream.Close ();
HttpWebResponse hresp = (HttpWebRespons e)hwr.GetRespon se();
}
catch (Exception ee)
{
bool ignoreme = (ee.Message == "");
retval = false;
}
return retval;
}
private static string GetPostDataSoap 11()
{
StringBuilder postData = new StringBuilder(" ");// "firstone=" + "";
postData.Append ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
postData.Append ("<soap:Envelop e xmlns:xsi=\"htt p://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"htt p://www.w3.org/2001/XMLSchema\" xmlns:soap=\"ht tp://schemas.xmlsoap .org/soap/envelope/\">\r\n");
postData.Append (" <soap:Body>\r\n ");
postData.Append (" <UpdateInformat ion xmlns=\"http://mylocation/\">\r\n");
postData.Append Format(null, " <WhenSent>{0} </WhenSent>\r\n", DateTime.Now);
postData.Append Format(null, " <StatusMessage> {0}</StatusMessage>\ r\n", "Nothing to report");
postData.Append (" </UpdateInformati on>\r\n");
postData.Append (" </soap:Body>\r\n" );
postData.Append ("</soap:Envelope>\ r\n");
return postData.ToStri ng();
}[/code]
Comment