如何通过Tidhttp模块使用xml参数在Delphi中执行Get请求?

更新于
2026-10-05 03:37:39
2阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计415个文字,预计阅读时间需要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}&params={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');

本文共计415个文字,预计阅读时间需要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}&params={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');