TIdHttp自定义标头重定向

时间:2019-07-09 10:44:01

标签: delphi indy idhttp

我在处理带有自定义标头的重定向时遇到问题。通过永久移动的HTTP / 1.1 301确认了对REST-API终结点的一些请求。

这是我的代码:

procedure APIRequest(const url, token: string; request, response: TStream);
var
  http: TIdHttp;
  ssl: TIdSSLIOHandlerSocketOpenSSL;
begin
  http := TIdHttp.Create(nil);
  ssl := TIdSSLIOHandlerSocketOpenSSL.Create(http);
  try
    ssl.SSLOptions.SSLVersions := [sslvTLSv1_2];
    http.IOHandler := ssl;

    http.HandleRedirects := true;

    http.Request.CustomHeaders.Clear;
    http.Request.Accept := 'application/json';
    http.Request.CustomHeaders.FoldLines := false; // without http status code 400
    http.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + token;

    try
      if request = nil then
        http.Get(url, response)
      else
        http.Post(url, request, response);
    except
      on e: EIdHTTPProtocolException do begin
        if response is TStringStream then
          (response as TStringStream).WriteString(e.ErrorMessage); // errormessage is the actual http-body
      end;
    end;
  finally
    http.Free;
  end;
end;

在必须处理重定向之前,它工作得很好。它引发以下错误:

  

HTTP / 1.1 400身份验证信息未正确给出   格式。检查授权标头的值。

我认为自定义标头在重定向后会丢失。

我试图看一下请求标头,但是提琴手4没有跟踪我的过程。

有人知道怎么做吗?请指出正确的方向。预先感谢。

更新20190710 09:40

好的,自定义标题不会丢失。我需要一个cookie管理器吗?

这是雷米建议的TIdLogFile日志:

Stat Connected.
Sent 10.07.2019 09:35:52: GET /api/v1.0/Products HTTP/1.1
Authorization: Bearer mysecrettoken
Host: mysecrethost.de
Accept: application/json
Accept-Encoding: identity
User-Agent: Mozilla/3.0 (compatible; Indy Library)


Recv 10.07.2019 09:35:52: HTTP/1.1 301 Moved Permanently
Location: https://mysecretsubdomain.blob.core.windows.net/products/Products.json?sv=2018-03-28&sr=b&sig=%2F8Y4qCtsLhJUYHJVHgM2cvlyg6hLI4pLg16FEmvQzsY%3D&st=2019-07-10T07%3A20%3A52Z&se=2019-07-10T07%3A50%3A52Z&sp=r
Request-Context: appId=cid-v1:49572fba-119a-44ce-80c2-e12dd8810c9a
Date: Wed, 10 Jul 2019 07:35:52 GMT
Content-Length: 0


Stat Disconnected.
Stat Connected.
Sent 10.07.2019 09:35:52: GET /products/Products.json?sv=2018-03-28&sr=b&sig=%2F8Y4qCtsLhJUYHJVHgM2cvlyg6hLI4pLg16FEmvQzsY%3D&st=2019-07-10T07%3A20%3A52Z&se=2019-07-10T07%3A50%3A52Z&sp=r HTTP/1.1
Authorization: Bearer mysecrettoken
Host: mysecretsubdomain.blob.core.windows.net
Accept: application/json
Accept-Encoding: identity
User-Agent: Mozilla/3.0 (compatible; Indy Library)


Recv 10.07.2019 09:35:52: HTTP/1.1 400 Authentication information is not given in the correct format. Check the value of Authorization header.
Content-Length: 298
Content-Type: application/xml
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: ec5b36eb-c01e-0112-69f2-36b58a000000
Date: Wed, 10 Jul 2019 07:35:52 GMT

<?xml version="1.0" encoding="utf-8"?>
<Error><Code>InvalidAuthenticationInfo</Code><Message>Authentication information is not given in the correct format. Check the value of Authorization header.
RequestId:ec5b36eb-c01e-0112-69f2-36b58a000000
Time:2019-07-10T07:35:52.8167021Z</Message></Error>
Stat Disconnected.

0 个答案:

没有答案