'Stamps.com webservice Error - (500) Internal Server Error

I am getting the Error below when calling the Authenticate method of Stamps.com webservice.

The remote server returned an error: (500) Internal Server Error.

Here is the SOAP 1.1 definition of the request.

POST /swsim/SwsimV45.asmx HTTP/1.1
Host: swsim.testing.stamps.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://stamps.com/xml/namespace/2015/05/swsim/swsimv45/AuthenticateUser"

<?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>
    <AuthenticateUser xmlns="http://stamps.com/xml/namespace/2015/05/swsim/swsimv45">
      <Credentials>
        <IntegrationID>guid</IntegrationID>
        <Username>string</Username>
        <Password>string</Password>
      </Credentials>
    </AuthenticateUser>
  </soap:Body>
</soap:Envelope>

Here is my code calling the service.

private void button2_Click(object sender, EventArgs e)
{
    CallStampsService();
}

private void CallStampsService()
{
    HttpWebRequest request = CreateHTTPWebRequest();
    XmlDocument soapEnvelopeXml = new XmlDocument();
    soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
    <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" 
       xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
       xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
       xmlns:tns=""http://stamps.com/xml/namespace/2015/05/swsim/swsimv45"">
    <soap:Body>
        <AuthenticateUser>
           <Credentials>
            <IntegrationID>my_integration_id</IntegrationID>
            <Username>my_username</Username>
            <Password>my_password</Password>
           </Credentials>
        </AuthenticateUser>
    </soap:Body>
    </soap:Envelope>");

    using (Stream stream = request.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }

    using (WebResponse response = request.GetResponse())
    {
        using (StreamReader rd = new StreamReader(response.GetResponseStream()))
        {
            string soapResult = rd.ReadToEnd();
            MessageBox.Show(soapResult);
        }
    }

}

public static HttpWebRequest CreateHTTPWebRequest()
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://swsim.testing.stamps.com/swsim/SwsimV45.asmx");
    webRequest.Headers.Add(@"SOAPAction: " + @"http://stamps.com/xml/namespace/2015/05/swsim/swsimv45/AuthenticateUser");
    webRequest.ContentType = "text/xml;charset=utf-8";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    return webRequest;
}

}

I contacted Stamps.com and their support said they can't offer much assistance regarding the code, but indicated that the following error appears on their end.

<faultstring>Server did not recognize the value of HTTP Header SOAPAction: http://stamps.com/xml/namespace/2015/05/swsim/swsimv45.</faultstring>

So I inspected my Header object and got this.

{SOAPAction: http://stamps.com/xml/namespace/2015/05/swsim/swsimv45/AuthenticateUser
Content-Type: text/xml;charset=utf-8
Accept: text/xml
Host: swsim.testing.stamps.com
Content-Length: 580
Expect: 100-continue
Connection: Keep-Alive
}

The application fails at this line.

WebResponse response = request.GetResponse()

So I inspected the innerXML and got this.

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soap:Envelope
    xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"
    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
    xmlns:tns=\"http://stamps.com/xml/namespace/2015/05/swsim/swsimv45\">
    <soap:Body>
        <AuthenticateUser>
            <Credentials>
                <IntegrationID>my_integrationID</IntegrationID>
                <Username>my_username</Username>
                <Password>my_password</Password>
            </Credentials>
        </AuthenticateUser>
    </soap:Body>
</soap:Envelope>

I do not know where I am going wrong.



Solution 1:[1]

I feel your pain. I've used the method below with success:

-- POST

https://swsim.stamps.com/swsim/swsimv42.asmx

-- HEADER

SOAPAction: http://stamps.com/xml/namespace/2015/01/swsim/swsimv42/AuthenticateUser
Content-type: text/xml

-- BODY

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<soap:Body>
    <AuthenticateUser xmlns="http://stamps.com/xml/namespace/2015/01/swsim/swsimv42">
      <Credentials>
        <IntegrationID>XXXXXXX</IntegrationID>
        <Username>XXXXXXX</Username>
        <Password>XXXXXXX</Password>
      </Credentials>
    </AuthenticateUser>
</soap:Body>
</soap:Envelope>

Please note there are numerous versions of the stamps.com namespaces. Some work great, others are frustrating.

Solution 2:[2]

Yes, Stamps docs are VERY confusing. Especially, in their bunch of WSDL versions. But their support's answer was surprisingly informative. The http://stamps.com/xml/namespace/2015/05/swsim/swsimv45 xmls reference is in the wrong place. But they didn't tell you. And why.

xmls references a namespace, class, where stored a definition for the entity that referenced it.

For example,<AuthenticateUser xmlns="http://stamps.com/xml/namespace/2015/01/swsim/swsimv42"> tells that a definition of AuthenticateUser is in http://stamps.com/xml/namespace/2015/01/swsim/swsimv42 namespace. A non-global SOAP element (custom) located in a custom namespace. Everything is OK here.

Or <soap:Envelope ... mlns:tns=""http://stamps.com/xml/namespace/2015/05/swsim/swsimv45""> tells that soap element's description should be used from http://stamps.com/xml/namespace/2015/05/swsim/swsimv45 namespace. But the soap:Envelope element is global for SOAP. And a local namespace doesn't have a definition for it. That's why there was an error.

To fix the issue you need to reference that namespace in a proper place. Move it from <soap:Envelope ...> to <AuthenticateUser ...>. And your SOAP envelope will be OK.

P. S. Know, that the question is very old. But it wasn't answered and could help somebody who googled it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sperjesi
Solution 2 PIoneer_2