[英]Weird indy redirect post
I'm trying to send a post request using Indy but I am facing some problems. 我正在尝试使用Indy发送发帖请求,但遇到了一些问题。 In a form I have TIdHTTP, a TIdSSLIOHandlerSocketOpenSSL and a TIdCookieManager whith these properties:
在某种形式下,我具有TIdHTTP,TIdSSLIOHandlerSocketOpenSSL和TIdCookieManager,它们具有以下属性:
TIdHTTP: TIdHTTP:
IdHTTP1.IOHandler := FSSLIO;
IdHTTP1.AllowCookies := True;
IdHTTP1.HandleRedirects := True;
IdHTTP1.ProxyParams.BasicAuthentication := False;
IdHTTP1.ProxyParams.ProxyPort := 0;
IdHTTP1.Request.ContentLength := -1;
IdHTTP1.Request.Accept := 'text/html, */*';
IdHTTP1.Request.BasicAuthentication := False;
IdHTTP1.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
IdHTTP1.HTTPOptions := [hoKeepOrigProtocol, hoForceEncodeParams];
IdHTTP1.OnRedirect := IdHTTP1Redirect;
IdHTTP1.CookieManager := IdCookieManager1;
TIdSSLIOHandlerSocketOpenSSL: default values TIdSSLIOHandlerSocketOpenSSL:默认值
TIdCookieManager: default values TIdCookieManager:默认值
OnRedirect procedure: OnRedirect程序:
Handled := True;
In a button the following request: 在一个按钮中,以下请求:
Params := TStringStream.Create('asdf=asdf',TEncoding.UTF8);
edtmemo1.Text := IdHTTP1.Post('https://www.detran.mg.gov.br/habilitacao/1-habilitacao-quero-ser-condutor/consultar-resultado-exame-legislacao/-/busca_resultado_exames/', Params);
Params.Free;
But returns error with response code 301, but the wierd part is that the location is the same url that I am trying to send, so it enter in an infinite loop. 但是返回响应代码为301的错误,但更奇怪的是该位置与我尝试发送的网址相同,因此它进入了无限循环。
Response 响应
HTTP/1.1 301 Moved Permanently
Date: Thu, 31 Aug 2017 20:23:32 GMT
Server: Apache
X-Powered-By: PHP/5.3.5
P3P: CP="A politica de privacidade deve estar disponivel no site ou pode ser solicitada via fale conosco."
Set-Cookie: SECCCAKEPHP=e19ttp30m5380ih41qal0gipg2; expires=Sat, 09-Sep-2017 04:23:32 GMT; path=/
Location: https://www.detran.mg.gov.br/habilitacao/1-habilitacao-quero-ser-condutor/consultar-resultado-exame-legislacao/-/busca_resultado_exames/
Cache-Control: max-age=604800
Expires: Thu, 07 Sep 2017 20:23:32 GMT
Content-Length: 0
Content-Type: text/html; charset=utf-8
Curl test: 卷曲测试:
curl -vv -X POST -F 'asdf=asdf' https://www.detran.mg.gov.br/habilitacao/1-habilitacao-quero-ser-condutor/consultar-resultado-exame-legislacao/-/busca_resultado_exames/
I tried the same request using curl and works with response code 200. Any idea of what could be happening? 我使用curl尝试了相同的请求,并使用了响应代码200。可能会发生什么的任何想法?
If you read the curl documentation , you will see that the -F
option posts data in multipart/form-data
format: 如果阅读curl文档 ,您将看到
-F
选项以multipart/form-data
格式发布数据:
-F, --form
-F,--form
(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button.
(HTTP)这使curl可以模拟用户按下提交按钮时所填写的表单。 This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388 .
这会导致使用RFC 2388的Content-Type multipart / form-data卷曲到POST数据 。 This enables uploading of binary files etc.
这样可以上传二进制文件等。
However, your TIdHTTP
code is posting data that is in application/x-www-form-urlencoded
format instead. 但是,您的
TIdHTTP
代码TIdHTTP
发布application/x-www-form-urlencoded
格式的数据。 And more importantly, you are not setting the TIdHTTP.Request.ContentType
property to 'application/x-www-form-urlencoded'
to match the data. 更重要的是,您没有将
TIdHTTP.Request.ContentType
属性设置为'application/x-www-form-urlencoded'
以匹配数据。 If you post a TStream
, you have to set the ContentType
accordingly. 如果发布
TStream
,则必须相应地设置ContentType
。
TIdHTTP
prefers application/x-www-form-urlencoded
data to be posted using a TStrings
-derived object (like TStringList
) instead of a TStream
, so it can ensure the data is encoded and formatted correctly, eg: TIdHTTP
倾向于使用TStrings
派生的对象(例如TStringList
)而不是TStream
来发布application/x-www-form-urlencoded
数据,因此它可以确保数据正确编码和格式化,例如:
var
Params: TStringList;
begin
...
Params := TStringList.Create;
try
Params.Add('asdf=asdf'); // <-- DO NOT url-encode the values here!
// the TStrings version of Post() will set the Request.ContentType
// to 'application/x-www-form-urlencoded' by default...
edtmemo1.Text := IdHTTP1.Post('https://www.detran.mg.gov.br/habilitacao/1-habilitacao-quero-ser-condutor/consultar-resultado-exame-legislacao/-/busca_resultado_exames/', Params);
finally
Params.Free;
end;
...
end;
TIdHTTP
prefers multipart/form-data
data to be posted using a TIdMultipartFormDataStream
object instead (unless you pass it another TStream
that contains pre-formatted MIME data in it). TIdHTTP
宁愿使用TIdMultipartFormDataStream
对象来发布multipart/form-data
数据(除非您传递给它另一个包含预格式化的MIME数据的TStream
)。
So, to match what your curl command is sending, try this instead: 因此,要匹配curl命令发送的内容,请尝试以下操作:
uses
..., IdMultipartFormData;
var
Params: TIdMultipartFormDataStream;
begin
...
Params := TIdMultipartFormDataStream.Create;
try
Params.AddFormField('asdf', 'asdf', 'utf-8');
// the TIdMultipartFormDataStream version of Post() will set the
// Request.ContentType to 'multipart/form-data' with a suitable
// MIME 'boundary' attribute for you...
edtmemo1.Text := IdHTTP1.Post('https://www.detran.mg.gov.br/habilitacao/1-habilitacao-quero-ser-condutor/consultar-resultado-exame-legislacao/-/busca_resultado_exames/', Params);
finally
Params.Free;
end;
...
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.