accessing webservice with soap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    accessing webservice with soap

    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.
    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>
    Now I tried to produce this using HttpWebRequest and sent the following(pulle d from a packet watcher):
    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>
    For return result:
    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]
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    And in typical fashion, the problem turned out to be the one thing I thought for sure was right:
    http://localhost/testdb/UnitReporting.a smx/UpdateInformati on
    is used for regular POST, while
    http://localhost/testdb/UnitReporting.a smx
    is used for soap connections.

    REDIT: I guess I had read something wrong, the best working format I have found for soap requests is this:
    string datetimeformat = @"yyyy-MM-ddTHH:mm:ss"

    Everything else results in the original "local" time transfered to be normalized to the server's local timezone

    Comment

    Working...