我已经成功地使用 Delphi 2010 发出 http get 请求,但是对于一个需要名为“xml”的参数的服务,请求失败并出现“HTTP/1.1 400 Bad Request”错误。
我注意到调用相同的服务并省略“xml”参数是有效的。
我已经尝试了以下但没有成功:
HttpGet('http://localhost/Service/Messaging.svc/SendReports/PDF?xml=<?xml version="1.0"?><email><message><to>email@internal.com</to><from>from@internal.com</from></message></email>&id=42&profile=A1');
...
function TReportingFrame.HttpGet(const url: string): string;
var
responseStream : TMemoryStream;
html: string;
HTTP: TIdHTTP;
begin
try
try
responseStream := TMemoryStream.Create;
HTTP := TIdHTTP.Create(nil);
HTTP.OnWork:= HttpWork;
HTTP.Request.ContentType := 'text/xml; charset=utf-8';
HTTP.Request.ContentEncoding := 'utf-8';
HTTP.HTTPOptions := [hoForceEncodeParams];
HTTP.Request.CharSet := 'utf-8';
HTTP.Get(url, responseStream);
SetString(html, PAnsiChar(responseStream.Memory), responseStream.Size);
result := html;
except
on E: Exception do
Global.LogError(E, 'ProcessHttpRequest');
end;
finally
try
HTTP.Disconnect;
except
end;
end;
end;
使用重命名为任何其他名称的参数名称“xml”调用相同的 url,例如具有与上述相同值的“xml2”或“name”也可以。我也尝试了字符集的多种组合,但我认为 indy 组件正在内部更改它。
编辑
服务期望:
[WebGet(UriTemplate = "SendReports/{format=pdf}?report={reportFile}¶ms={jsonParams}&xml={xmlFile}&profile={profile}&id={id}")]
有没有人有这方面的经验?
谢谢
最佳答案
通过 URL 传递参数数据时需要对其进行编码,TIdHTTP
不会为您编码 URL,例如:
http.Get(TIdURI.URLEncode('http://localhost/Service/Messaging.svc/SendReports/PDF?xml=<?xml version="1.0"?><email><message><to>email@internal.com</to><from>from@internal.com</from></message></email>&id=42&profile=A1'));
或者:
http.Get('http://localhost/Service/Messaging.svc/SendReports/PDF?xml=' + TIdURI.ParamsEncode('<?xml version="1.0"?><email><message><to>email@internal.com</to><from>from@internal.com</from></message></email>') + '&id=42&profile=A1');
关于delphi - 如何使用 Tidhttp 发出带有名为 xml 的参数的 Get 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17856133/