BPost 地址验证 C# REST 调用不成功

BPost address validation C# REST call no success

我正在尝试根据 Visual Studio C# 中的 BPost API 验证地址。 这是我第一次使用 Web 服务。

我在 PHP 中找到了由 Spatie 创建的示例代码,并将它 运行 放在我的计算机上的 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;
        }

现在我得到一个“False”作为 IsSuccessStatusCode。 解决问题的最佳下一步是什么?

您可能必须向该服务验证您自己的身份。如果您使用 Postman 发送此请求,您将收到带有 Access Denied 正文的 403。

当输入格式不正确时,Postman 确实指出错误 403。 在 PHP 代码中引入 var_dump(json_encode($validateAddressesRequest->getBody())) 有助于找到正确的语法。

为了将来参考,这是从 Postman 生成的现在有效的代码。

        Console.WriteLine("Start");

        var client = new RestClient("https://webservices-pub.bpost.be/ws/ExternalMailingAddressProofingCSREST_v1/address/validateAddresses");
        String input = "{\"ValidateAddressesRequest\":{\"AddressToValidateList\":{\"AddressToValidate\":[{\"@id\":0,\"PostalAddress\":{\"DeliveryPointLocation\":{\"StructuredDeliveryPointLocation\":{\"StreetName\":\"Kaaistrat\",\"StreetNumber\":\"1\",\"BoxNumber\":\"\"}},\"PostalCodeMunicipality\":{\"StructuredPostalCodeMunicipality\":{\"PostalCode\":\"8400\",\"MunicipalityName\":\"Oostende\"}}},\"DeliveringCountryISOCode\":\"BELGIE\"},{\"@id\":1,\"PostalAddress\":{\"DeliveryPointLocation\":{\"StructuredDeliveryPointLocation\":{\"StreetName\":\"Leanderhof\",\"StreetNumber\":\"\",\"BoxNumber\":\"\"}},\"PostalCodeMunicipality\":{\"StructuredPostalCodeMunicipality\":{\"PostalCode\":\"8550\",\"MunicipalityName\":\"Zwevegem\"}}},\"DeliveringCountryISOCode\":\"BELGIE\"}]},\"ValidateAddressOptions\":[]}}";
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Cookie", "AWSALB=2BqLxFZuWQumzA1q6UhL1Pza0w8Lq3FGCUfB2+aqjOYulz1gvQUfXgRLps4I3FEhyb/TRyZaAPxCaHWZ8ISg2/Mq5BRCZX+5NqnVll7EwiOkdSB9sIf9EfcWzvJI; AWSALBCORS=2BqLxFZuWQumzA1q6UhL1Pza0w8Lq3FGCUfB2+aqjOYulz1gvQUfXgRLps4I3FEhyb/TRyZaAPxCaHWZ8ISg2/Mq5BRCZX+5NqnVll7EwiOkdSB9sIf9EfcWzvJI; webservices-pub.bpost.be=2936274954.64288.0000");
        request.AddParameter("application/json", input, ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);

        Console.Read();