2016-02-12 84 views
0

我在調用Stamps.com webservice的Authenticate方法時遇到以下錯誤。Stamps.com webservice錯誤 - (500)內部服務器錯誤

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

這是請求的SOAP 1.1定義。

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> 

這是我的代碼調用服務。

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; 
} 

} 

我聯繫Stamps.com和他們的支持表示他們不能提供有關代碼很多幫助,但表示他們最終會出現以下錯誤。

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

所以我檢查了我的Header對象並得到了這個。

{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 
} 

應用程序在此行失敗。

WebResponse response = request.GetResponse()

所以我檢查了innerXML和得到這個。

<?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> 

我不知道我出錯的地方。

+0

如果您嘗試使用WCF客戶端進行連接。它工作嗎? – Agalo

+0

我沒有想到這一點。 WCF客戶端可以調用「asmx」Web服務嗎? –

+0

是的,您需要添加服務參考。 WCF客戶端可以連接到ASMX Web服務,並且是推薦的方式。 – Agalo

回答

1

我感覺到你的痛苦。我用下面的方法成功:

- POST

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

- 頁眉

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> 

請注意有無數版本stamps.com名稱空間。有些工作很棒,其他人則很沮喪。