首页 > 解决方案 > 重定向时的 TIdHttp 自定义标头

问题描述

我在处理带有自定义标头的重定向时遇到问题。使用 HTTP/1.1 301 Moved Permanently 确认对 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 身份验证信息的格式不正确。检查 Authorization 标头的值。

我假设自定义标头在跟随重定向时会丢失。

我试图查看请求标头,但提琴手 4 没有跟踪我的进程。

有谁知道如何做到这一点?请指出我正确的方向。提前致谢。

更新20190710 09:40

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

这是 Remy 建议的来自 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.

标签: delphiindyidhttp

解决方案


推荐阅读