如何通过Tidhttp组件以xml参数执行Get请求并实现复杂功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计423个文字,预计阅读时间需要2分钟。
我成功使用Delphi 2010开发http get请求,但针对需要名为xml的参数的服务,请求失败并出现HTTP / 1.1 400 Bad Request错误。我注意到即使服务端省略了xml参数,请求仍然有效。我试过多次。
我已成功使用Delphi 2010来发出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’,其值与上面相同也可以.我也尝试了charset的多种组合,但我认为indy组件正在内部更改它.
编辑
该服务预计:
[WebGet(UriTemplate = "SendReports/{format=pdf}?report={reportFile}¶ms={jsonParams}&xml={xmlFile}&profile={profile}&id={id}")]
有没有人有这方面的经验?
谢谢
您需要在通过URL传递参数数据时对其进行编码,TIdHTTP不会为您编码URL,例如: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'));
要么:
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');
本文共计423个文字,预计阅读时间需要2分钟。
我成功使用Delphi 2010开发http get请求,但针对需要名为xml的参数的服务,请求失败并出现HTTP / 1.1 400 Bad Request错误。我注意到即使服务端省略了xml参数,请求仍然有效。我试过多次。
我已成功使用Delphi 2010来发出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’,其值与上面相同也可以.我也尝试了charset的多种组合,但我认为indy组件正在内部更改它.
编辑
该服务预计:
[WebGet(UriTemplate = "SendReports/{format=pdf}?report={reportFile}¶ms={jsonParams}&xml={xmlFile}&profile={profile}&id={id}")]
有没有人有这方面的经验?
谢谢
您需要在通过URL传递参数数据时对其进行编码,TIdHTTP不会为您编码URL,例如: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'));
要么:
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');

