【发布时间】:2021-06-23 00:51:29
【问题描述】:
我正在尝试根据 Visual Studio C# 中的 BPost API 验证地址。 这是我第一次使用 Web 服务。
我找到了 Spatie 创建的 PHP 示例代码,并让它在我的计算机上以 WAMP 运行。 https://github.com/spatie/bpost-address-webservice 现在我想从 C# 中获得相同的功能。我没有转换成功。
这似乎是 PHP 代码的相关部分:
protected $client;
public function __construct()
{
$this->client = new Client([
'base_uri' => 'https://webservices-pub.bpost.be/ws/ExternalMailingAddressProofingCSREST_v1/',
]);
}
public function validateAddresses(ValidateAddressesRequest $validateAddressesRequest): ValidateAddressesResponse
{
$response = $this->client->request('POST', 'address/validateAddresses', [
'json' => $validateAddressesRequest->getBody(),
]);
return new ValidateAddressesResponse(
json_decode((string) $response->getBody(), true),
$validateAddressesRequest->addresses()
);
}
public function getBody(): array
{
$addresses = array_map(function (Address $address, int $i) {
return [
'@id' => $i,
'PostalAddress' => [
'DeliveryPointLocation' => [
'StructuredDeliveryPointLocation' => [
'StreetName' => $address->streetName,
'StreetNumber' => $address->streetNumber,
'BoxNumber' => $address->boxNumber,
],
],
'PostalCodeMunicipality' => [
'StructuredPostalCodeMunicipality' => [
'PostalCode' => $address->postalCode,
'MunicipalityName' => $address->municipalityName,
],
],
],
'DeliveringCountryISOCode' => $address->country,
];
}, $this->addresses, array_keys(array_values($this->addresses)));
return [
'ValidateAddressesRequest' => [
'AddressToValidateList' => [
'AddressToValidate' => $addresses,
],
'ValidateAddressOptions' => $this->options,
],
];
}
这是我迄今为止在 C# 中尝试过的:
static void Main(string[] args)
{
Console.WriteLine("Start");
var payload = "<@id>0</@id><PostalAddress><DeliveryPointLocation><StructuredDeliveryPointLocation><StreetName>Kaaistraat</StreetName><StreetNumber>1</StreetNumber><BoxNumber>1</BoxNumber>" +
"</StructuredDeliveryPointLocation></DeliveryPointLocation><PostalCodeMunicipality><StructuredPostalCodeMunicipality><PostalCode>8400</PostalCode>" +
"<MunicipalityName>Oostende</MunicipalityName></StructuredPostalCodeMunicipality></PostalCodeMunicipality><DeliveringCountryISOCode>BE</DeliveringCountryISOCode>";
HttpContent c = new StringContent(payload, Encoding.UTF8, "text/xml");
var t = Task.Run(() => PostURI(c));
t.Wait();
Console.WriteLine("Feedback: " + t.Result);
Console.WriteLine("End");
Console.ReadLine();
}
static async Task<string> PostURI(HttpContent c)
{
var client = new HttpClient();
client.BaseAddress = new Uri("https://webservices-pub.bpost.be/ws/ExternalMailingAddressProofingCSREST_v1/");
HttpResponseMessage result = await client.PostAsync("address/validateAddresses", c);
String response = result.IsSuccessStatusCode.ToString();
if (result.IsSuccessStatusCode)
{
response = result.StatusCode.ToString();
}
return response;
}
现在我得到一个“错误”作为 IsSuccessStatusCode。 排除故障以寻求解决方案的最佳下一步是什么?
【问题讨论】:
-
尝试在代码开头添加:ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;去年微软推出了一个安全更新,在服务器上禁用了 TLS 1.0 和 1.1,但没有改变客户端。因此,如果您的客户端上的默认 TLS 版本是 1.0 或 1.1,那么代码将不起作用。将默认值更改为 1.2 可能会解决问题。
-
谢谢jdweng。我添加了该行并运行,但它不会改变我的网络呼叫的响应。
-
我们需要确定 TLS 是否通过或问题出在哪里。最好的确定方法是使用像wireshark或fiddler这样的嗅探器。 TLS 发生在 HTTP 请求之前。因此,嗅探器将在成功请求时显示 TLS 部分,然后显示请求。如果没有请求 TLS 失败。检查 TLS 版本和证书块。证书块通过证书名称和加密模式列表从服务器发送到客户端。在您的情况下,TLS 失败 Net 版本不支持加密模式。您应该使用在操作系统中完成 TLS 的 Net 4.7.2 或更高版本。
标签: c# xml rest web-services post