【问题标题】:Connection refused Error on calling third party api调用第三方 api 时连接被拒绝错误
【发布时间】:2020-10-24 05:20:54
【问题描述】:

我正在使用 Angular 创建一个项目并调用第三方 Web api 发布请求,但该请求未正确连接并给出连接被拒绝 cors 错误。此 api 可与一些 api 测试工具一起使用..这是我的请求

vehicleCheck(data) {
  let newHeaders = { headers: new HttpHeaders({ 'x-api-key': "xxxxxx" }) };
  return this.http.post('https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles',data,newHeaders);
}

当我联系api的开发者时,开发者的回复是:

However I would advise you to install a ‘curl version’ to get a feel of how the authentication mechanism works and you should configure your code to follow something like below:

 

 

$curl -d '{"registrationNumber": "KC07PZG"}' https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles -H "Content-Type: application/json" -H "Insert Key No" -v

*   Trying ***.***.***.***...

* TCP_NODELAY set

* Connected to driver-vehicle-licensing.api.gov.uk (***.***.***.***) port 443 (#0)

* ALPN, offering h2

* ALPN, offering http/1.1

* successfully set certificate verify locations:

*   CAfile: /etc/ssl/cert.pem

  CApath: none

* TLSv1.2 (OUT), TLS handshake, Client hello (1):

* TLSv1.2 (IN), TLS handshake, Server hello (2):

* TLSv1.2 (IN), TLS handshake, Certificate (11):

* TLSv1.2 (IN), TLS handshake, Server key exchange (12):

* TLSv1.2 (IN), TLS handshake, Server finished (14):

* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):

* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):

* TLSv1.2 (OUT), TLS handshake, Finished (20):

* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):

* TLSv1.2 (IN), TLS handshake, Finished (20):

* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384

* ALPN, server did not agree to a protocol

* Server certificate:

*  subject: jurisdictionCountryName=GB; businessCategory=Government Entity; serialNumber=Government Entity; C=GB; ST=Vale of Glamorgan; L=Swansea; O=Driver & Vehicle Licensing Agency; CN=driver-vehicle-licensing.api.gov.uk

*  start date: Jan 16 13:29:38 2020 GMT

*  expire date: Jan 16 13:39:00 2022 GMT

*  subjectAltName: host "driver-vehicle-licensing.api.gov.uk" matched cert's "driver-vehicle-licensing.api.gov.uk"

*  issuer: C=BM; O=QuoVadis Limited; CN=QuoVadis EV SSL ICA G3

*  SSL certificate verify ok.

> POST /vehicle-enquiry/v1/vehicles HTTP/1.1

> Host: driver-vehicle-licensing.api.gov.uk

> User-Agent: curl/7.64.1

> Accept: */*

> Content-Type: application/json

> x-api-key:Insert Key No.

> Content-Length: 33

> 

* upload completely sent off: 33 out of 33 bytes

< HTTP/1.1 200 OK

< Date: Mon, 29 Jun 2020 15:50:22 GMT

< Content-Type: application/json;charset=UTF-8

< Content-Length: 390

< Connection: keep-alive

< x-amzn-RequestId: c5de3789-5ba7-4382-b1c3-a94758ffa741

< strict-transport-security: max-age=15724800; includeSubDomains

< x-amzn-Remapped-content-length: 433

< x-amzn-Remapped-connection: close

< x-amz-apigw-id: O5cVyEPHrPEFsPg=

< vary: Accept-Encoding

< server-timing: intid;desc=24c9d67136bad3f5

< X-Amzn-Trace-Id: Root=1-5efa0dbe-8d4648d06334b2984ec0d940;Sampled=0

< x-amzn-Remapped-date: Mon, 29 Jun 2020 15:50:22 GMT

< Via: 1.1 lon1-bit30

< Set-Cookie: TS01e7f829=01dec1a26847c3c75c6e05efc84194a77e65a82b22573475dde4075767837458d6467cb76cf72d9ac595baa02a2f8358d3ed2db841; Path=/; Secure; HTTPOnly

< 

* Connection #0 to host driver-vehicle-licensing.api.gov.uk left intact

{"registrationNumber":"KC07PZG","co2Emissions":129,"engineCapacity":1995,"markedForExport":false,"fuelType":"DIESEL","motStatus":"Valid","colour":"GREY","make":"BMW","typeApproval":"M1","yearOfManufacture":2007,"taxDueDate":"2020-10-01","taxStatus":"Taxed","dateOfLastV5CIssued":"2017-10-03","motExpiryDate":"2021-07-05","wheelplan":"2 AXLE RIGID BODY","monthOfFirstRegistration":"2007-05"}* Closing connection 0

请帮助我在这里缺少什么

【问题讨论】:

    标签: javascript angular api


    【解决方案1】:

    你必须在这样的数据之后添加你的标题。

    vehicleCheck(data) {
      let newHeaders = { headers: new HttpHeaders({ 'x-api-key': "xxxxxx" }) };
      return this.http.post('https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles',data, newHeaders);
    }
    

    【讨论】:

    • console.log() 说什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2021-09-13
    • 2021-09-05
    • 1970-01-01
    相关资源
    最近更新 更多