我试图让USCG端口状态信息交换API工作,到目前为止没有运气。我也可以在网上找到关于该服务的几乎零信息。
有一个主要的USCG页面:
https://cgmix.uscg.mil/XML/Default.aspx
和ASMX / WSDL:
https://cgmix.uscg.mil/xml/PSIXData.asmx
https://cgmix.uscg.mil/xml/PSIXData.asmx?WSDL
我试图使用的方法是getVesselSummary。 API看起来很简单,但到目前为止,我只能得到一个空响应。所以在这一点上我想知道它是否需要某种身份验证,即使我没有在网上看到任何提及。
我将他们的WSDL添加到Visual Studio作为Web引用,并且代理类很好地构建。
我试过填写一些参数,只有一个参数。结果总是一样的(下面的截图):
var vs = new uscg.psix.PSIXData();
var data = vs.getVesselSummary("650617", "CITY OF CHICAGO", "WDC2646", "", "", "UNITED STATES", "", "1982");
我知道这艘船的存在是因为我可以使用他们的在线界面找到相同的搜索条件:
编辑
Visual Studio Web Reference客户端创建的SOAP / XML显然是个问题。当我使用Soap UI连接到服务并以这种方式创建请求时,它返回数据。
所以现在问题是如何让Visual Studio / C#返回此服务喜欢的标记?
这工作(肥皂用户界面)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cgm="http://cgmix.uscg.mil">
<soapenv:Header/>
<soapenv:Body>
<cgm:getVesselSummary>
<cgm:VesselID>650617</cgm:VesselID>
<cgm:VesselName></cgm:VesselName>
<cgm:CallSign></cgm:CallSign>
<cgm:VIN></cgm:VIN>
<cgm:HIN></cgm:HIN>
<cgm:Flag></cgm:Flag>
<cgm:Service></cgm:Service>
<cgm:BuildYear></cgm:BuildYear>
</cgm:getVesselSummary>
</soapenv:Body>
</soapenv:Envelope>
这不起作用 - Visual Studio的请求
<?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>
<getVesselSummary xmlns="http://cgmix.uscg.mil">
<VesselID>650617</VesselID>
<VesselName>
</VesselName>
<CallSign>
</CallSign>
<VIN>
</VIN>
<HIN>
</HIN>
<Flag>
</Flag>
<Service>
</Service>
<BuildYear>
</BuildYear>
</getVesselSummary>
</soap:Body>
</soap:Envelope>
好的,所以问题是双重的。
一:尽管我使用https将WSDL地址输入到添加Web引用对话框中,但Visual Studio以其无限的智慧决定将其存储为http。
二:海岸警卫队网站上存在某种证书错误。所以我需要为System.Net.ServicePointManager.ServerCertificateValidationCallback
添加一个处理程序来忽略它。
我的测试页面的完整代码,我正在这里倾倒,所以我可以继续前进。不要忘记使用陈述!:
using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web.UI;
namespace CGIX_2
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = MyCertHandler;
try
{
var sr = new mil.uscg.psix.PSIXData();
var data = sr.getVesselSummaryXMLString("", "A STEVE CROWLEY", "", "", "", "", "", "");
}
catch (Exception ex)
{
}
}
static bool MyCertHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors error)
{
// Ignore errors
return true;
}
}
}