Microsoft Azure版本2转换CORS错误(307临时重定向“对预检的响应无效(重定向)”)

时间:2018-08-01 06:42:46

标签: azure curl cors translate

我正在尝试将V2(不建议使用)Microsoft Azure Translate与POST请求一起使用,并在开发工具网络选项卡中显示"Response for preflight is invalid (redirect)"并显示307 Temporary Redirect错误。参见屏幕截图1和2,两者都是来自WebApp Javascript的调用。

screenshot 1

screenshot 2

我使用CURL诊断此问题。

  1. 首先使用Curl而不使用URL [https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames?locale=en]进行预检请求。它会以有效的响应成功。

    在下面的附录A

    中查看通话和完整的答复

    一个收获是-预检请求似乎是自愿的,它使Web客户端可以知道他们的实际呼叫是否会成功。已确认-[https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests

  2. 将URL更改为[https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames/?locale=en](请注意查询参数之前的斜杠),并且失败并显示

     HTTP/1.1 405 Method Not Allowed
     Allow: GET
    

    在下面的附录B

  3. 上查看通话和完整的回复
  4. 更改为使用URL [https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames?locale=en]发送预检请求,其中包含

    HTTP/1.1 307 Temporary Redirect
    Content-Length: 1997
    Content-Type: text/html; charset=UTF-8
    Location: https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames/?locale=en
    

    此响应表明,您应该改为引用其他位置。注意查询参数之前的斜杠。

    在下面的附录C

  5. 上查看通话和完整回复
  6. 在预检请求中添加-L,以强制CURL遵循重定向。它会产生与上述2相同的错误:

    HTTP/1.1 405 Method Not Allowed
    Allow: GET
    

    在下面的附录D

  7. 中查看通话和完整的回复

鉴于所有这些,看来应用程序错误(请参见屏幕截图)等同于上面的步骤3和4。步骤3是第一个屏幕截图(进行了OPTION调用并返回了新的LOCATION);第4步是第二个屏幕截图(重定向的OPTION位置被错误调用,并导致错误)。不幸的是,浏览器在返回有关此错误的有用信息方面表现很差。我的意思是[[https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests_and_redirects]表示“目前大多数浏览器不支持针对预检请求进行的重定向”。我以为是问题所在,但似乎正在遵循 重定向。

实际上,我正试图将其发布到[https://api.microsofttranslator.com/V2/Http.svc/TranslateArray],尽管这也遇到了同样的问题。

现在,我已经完成了所有这些工作(其中大部分是在创建此帖子期间进行的),看来我已经解决了这个问题。因此,我的大部分问题都得到了回答。尽管要遵循SO准则,但我将发布一个问题,或者将其留在此处,以供我与Microsoft讨论时以及那些偶然发现同一问题并正在寻找答案的人参考。

我的问题是-这听起来像Microsoft的某些错误配置吗?那可能是什么?欢迎其他任何想法,建议或意见!

附录A

    curl -v -X POST \
      'https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames?locale=en' \
      -H 'Cache-Control: no-cache' \
      -H 'Content-Type: text/xml' \
      -H 'Ocp-Apim-Subscription-Key: 67890a67890a67890a67890a' \
      -H 'Postman-Token: a6d26f3b-7e55-4e47-a262-5b8d4e912ea6' \
      -d '<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <string>zh</string>
        <string>en</string>
    </ArrayOfstring>'

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 111.221.29.48...
* TCP_NODELAY set
* Connected to api.microsofttranslator.com (111.221.29.48) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* 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, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: CN=*.microsofttranslator.com
*  start date: Feb 16 20:10:18 2018 GMT
*  expire date: Feb 16 20:10:18 2020 GMT
*  subjectAltName: host "api.microsofttranslator.com" matched cert's "*.microsofttranslator.com"
*  issuer: C=US; ST=Washington; L=Redmond; O=Microsoft Corporation; OU=Microsoft IT; CN=Microsoft IT TLS CA 2
*  SSL certificate verify ok.
> POST /V2/Http.svc/GetLanguageNames?locale=en HTTP/1.1
> Host: api.microsofttranslator.com
> User-Agent: curl/7.54.0
> Accept: */*
> Cache-Control: no-cache
> Content-Type: text/xml
> Ocp-Apim-Subscription-Key: 67890a67890a67890a67890a
> Postman-Token: a6d26f3b-7e55-4e47-a262-5b8d4e912ea6
> Content-Length: 211
>
* upload completely sent off: 211 out of 211 bytes
< HTTP/1.1 200 OK
< Cache-Control: private, max-age=604800
< Content-Length: 197
< Content-Type: application/xml; charset=utf-8
< Expires: Wed, 08 Aug 2018 03:57:55 GMT
< Last-Modified: Wed, 01 Aug 2018 03:57:55 GMT
< ETag: C75894C47/31/2018 8:57:55 PM
< X-MS-Trans-Info: 0642.V2_Rest.GetLanguageNames.4BFB9C9D
< Date: Wed, 01 Aug 2018 03:57:55 GMT
<
* Connection #0 to host api.microsofttranslator.com left intact
<ArrayOfstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><string>Chinese</string><string>English</string></ArrayOfstring>

附录B

curl -v -X POST \
  'https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames/?locale=en' \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: text/xml' \
  -H 'Ocp-Apim-Subscription-Key: secret' \
  -H 'Postman-Token: a6d26f3b-7e55-4e47-a262-5b8d4e912ea6' \
  -d '<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <string>zh</string>
    <string>en</string>
</ArrayOfstring>'

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 111.221.29.48...
* TCP_NODELAY set
* Connected to api.microsofttranslator.com (111.221.29.48) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* 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, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: CN=*.microsofttranslator.com
*  start date: Feb 16 20:10:18 2018 GMT
*  expire date: Feb 16 20:10:18 2020 GMT
*  subjectAltName: host "api.microsofttranslator.com" matched cert's "*.microsofttranslator.com"
*  issuer: C=US; ST=Washington; L=Redmond; O=Microsoft Corporation; OU=Microsoft IT; CN=Microsoft IT TLS CA 2
*  SSL certificate verify ok.
> POST /V2/Http.svc/GetLanguageNames/?locale=en HTTP/1.1
> Host: api.microsofttranslator.com
> User-Agent: curl/7.54.0
> Accept: */*
> Cache-Control: no-cache
> Content-Type: text/xml
> Ocp-Apim-Subscription-Key: 67890a67890a67890a67890a
> Postman-Token: a6d26f3b-7e55-4e47-a262-5b8d4e912ea6
> Content-Length: 211
>
* upload completely sent off: 211 out of 211 bytes
< HTTP/1.1 405 Method Not Allowed
< Allow: GET
< Content-Length: 1565
< Content-Type: text/html; charset=UTF-8
< Date: Wed, 01 Aug 2018 03:52:42 GMT
<
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Service</p>
      <p>Method not allowed.</p>
    </div>
  </body>
* Connection #0 to host api.microsofttranslator.com left intact
</html>

附录C

curl --verbose -X OPTIONS \
  'https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames?locale=en' \
  -H "Origin: http://localhost:4000" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: X-Requested-With"

*   Trying 111.221.29.48...
* TCP_NODELAY set
* Connected to api.microsofttranslator.com (111.221.29.48) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* 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, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: CN=*.microsofttranslator.com
*  start date: Feb 16 20:10:18 2018 GMT
*  expire date: Feb 16 20:10:18 2020 GMT
*  subjectAltName: host "api.microsofttranslator.com" matched cert's "*.microsofttranslator.com"
*  issuer: C=US; ST=Washington; L=Redmond; O=Microsoft Corporation; OU=Microsoft IT; CN=Microsoft IT TLS CA 2
*  SSL certificate verify ok.
> OPTIONS /V2/Http.svc/GetLanguageNames?locale=en HTTP/1.1
> Host: api.microsofttranslator.com
> User-Agent: curl/7.54.0
> Accept: */*
> Origin: http://localhost:4000
> Access-Control-Request-Method: POST
> Access-Control-Request-Headers: X-Requested-With
>
< HTTP/1.1 307 Temporary Redirect
< Content-Length: 1997
< Content-Type: text/html; charset=UTF-8
< Location: https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames/?locale=en
< Date: Wed, 01 Aug 2018 04:21:51 GMT
<
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Service</p>
      <p xmlns="">There is no operation listening for <a href="https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames?locale=en">https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames?locale=en</a>, but there is an operation listening for <a href="https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames/?locale=en">https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames/?locale=en</a>, so you are being redirected there.</p>
    </div>
  </body>
* Connection #0 to host api.microsofttranslator.com left intact
</html>

附录D

curl --verbose -L -X OPTIONS \
  'https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames?locale=en' \
  -H "Origin: http://localhost:4000" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: X-Requested-With"

*   Trying 111.221.29.48...
* TCP_NODELAY set
* Connected to api.microsofttranslator.com (111.221.29.48) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* 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, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: CN=*.microsofttranslator.com
*  start date: Feb 16 20:10:18 2018 GMT
*  expire date: Feb 16 20:10:18 2020 GMT
*  subjectAltName: host "api.microsofttranslator.com" matched cert's "*.microsofttranslator.com"
*  issuer: C=US; ST=Washington; L=Redmond; O=Microsoft Corporation; OU=Microsoft IT; CN=Microsoft IT TLS CA 2
*  SSL certificate verify ok.
> OPTIONS /V2/Http.svc/GetLanguageNames?locale=en HTTP/1.1
> Host: api.microsofttranslator.com
> User-Agent: curl/7.54.0
> Accept: */*
> Origin: http://localhost:4000
> Access-Control-Request-Method: POST
> Access-Control-Request-Headers: X-Requested-With
>
< HTTP/1.1 307 Temporary Redirect
< Content-Length: 1997
< Content-Type: text/html; charset=UTF-8
< Location: https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames/?locale=en
< Date: Wed, 01 Aug 2018 04:25:02 GMT
<
* Ignoring the response-body
* Connection #0 to host api.microsofttranslator.com left intact
* Issue another request to this URL: 'https://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames/?locale=en'
* Found bundle for host api.microsofttranslator.com: 0x7fd3e961bb40 [can pipeline]
* Re-using existing connection! (#0) with host api.microsofttranslator.com
* Connected to api.microsofttranslator.com (111.221.29.48) port 443 (#0)
> OPTIONS /V2/Http.svc/GetLanguageNames/?locale=en HTTP/1.1
> Host: api.microsofttranslator.com
> User-Agent: curl/7.54.0
> Accept: */*
> Origin: http://localhost:4000
> Access-Control-Request-Method: POST
> Access-Control-Request-Headers: X-Requested-With
>
< HTTP/1.1 405 Method Not Allowed
< Allow: GET
< Content-Length: 1565
< Content-Type: text/html; charset=UTF-8
< Date: Wed, 01 Aug 2018 04:25:02 GMT
<
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Service</p>
      <p>Method not allowed.</p>
    </div>
  </body>
* Connection #0 to host api.microsofttranslator.com left intact
</html>

0 个答案:

没有答案