【发布时间】:2016-02-12 23:08:04
【问题描述】:
调用 Stamps.com 网络服务的 Authenticate 方法时出现以下错误。
远程服务器返回错误:(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 响应 = 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>
我不知道我哪里出错了。
【问题讨论】:
-
如果您尝试使用 WCF 客户端进行连接。有用吗?
-
我没想到。 WCF 客户端可以调用“asmx”Web 服务吗?
-
是的,您需要添加服务参考。 WCF 客户端可以连接到 ASMX Web 服务,是推荐的方式。
-
我会试试看的。
-
在添加对swsim.testing.stamps.com/swsim/SwsimV45.asmx 或stamps.com/xml/namespace/2015/05/swsim/swsimv45/… 的服务引用时出现错误“HTML 文档不包含Web 服务发现信息”
标签: c# xml web-services stamps.com