我目前正在尝试使用美国邮政服务的 Web 工具 API来验证用户的地址。但是,无论我使用何种信息组合或技术连接到 Web 服务,都会遇到相同的异常:
Unable to connect to the remote service
具有相同的内部异常
A connection attempt failed because the connected party did not properly respond after
a period of time, or established connection failed because connected host has failed to
respond 56.0.34.43:443
不过,我是WebClient
第一次使用 C# 对象。谁能看到我在这里做错了什么?
private const string USPS_USERID = "xxxxxxx";
private const string BASEURL = "https://secure.shippingapis.com/ShippingAPITest.dll";
try
{
string USPS = BASEURL + "?API=Verify&XML=<AddressValidateRequest USERID=\"" + USPS_USERID + "\">";
USPS += "<Address ID='0'>";
USPS += "<Address1>" + addressInfo[0] + "</Address1>";
USPS += "<Address2>" + addressInfo[1] + "</Address2>";
USPS += "<City>" + addressInfo[2] + "</City>";
USPS += "<State>" + addressInfo[3] + "</State>";
USPS += "<Zip5>" + addressInfo[4] + "</Zip5>";
USPS += "<Zip4></Zip4>";
USPS += "</Address></AddressValidateRequest>";
WebClient wsClient = new WebClient();
byte[] responseData = wsClient.DownloadData(USPS);
string response = string.Empty;
foreach (byte item in responseData)
{
response += (char) item;
}
return Json(response, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return null;
}