delphi – 如何使用Tidhttp使用名为xml的参数发出Get请求?

前端之家收集整理的这篇文章主要介绍了delphi – 如何使用Tidhttp使用名为xml的参数发出Get请求?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已成功使用Delphi 2010来发出http get请求,但对于一个需要名为’xml’的参数的服务,请求失败并出现’HTTP / 1.1 400 Bad Request’错误.

我注意到调用相同的服务并省略’xml’参数有效.

我试过以下但没有成功:

  1. 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');

  1. function TReportingFrame.HttpGet(const url: string): string;
  2. var
  3. responseStream : TMemoryStream;
  4. html: string;
  5. HTTP: TIdHTTP;
  6. begin
  7. try
  8. try
  9. responseStream := TMemoryStream.Create;
  10. HTTP := TIdHTTP.Create(nil);
  11. HTTP.OnWork:= HttpWork;
  12. HTTP.Request.ContentType := 'text/xml; charset=utf-8';
  13. HTTP.Request.ContentEncoding := 'utf-8';
  14. HTTP.HTTPOptions := [hoForceEncodeParams];
  15. HTTP.Request.CharSet := 'utf-8';
  16. HTTP.Get(url,responseStream);
  17. SetString(html,PAnsiChar(responseStream.Memory),responseStream.Size);
  18. result := html;
  19. except
  20. on E: Exception do
  21. Global.LogError(E,'ProcessHttpRequest');
  22. end;
  23. finally
  24. try
  25. HTTP.Disconnect;
  26. except
  27. end;
  28. end;
  29. end;

使用参数名称’xml’调用相同的url重命名为其他任何内容,例如’xml2’或’name’,其值与上面相同也可以.我也尝试了charset的多种组合,但我认为indy组件正在内部更改它.

编辑

该服务预计:

  1. [WebGet(UriTemplate = "SendReports/{format=pdf}?report={reportFile}&params={jsonParams}&xml={xmlFile}&profile={profile}&id={id}")]

有没有人有这方面的经验?

谢谢

解决方法

您需要在通过URL传递参数数据时对其进行编码,TIdHTTP不会为您编码URL,例如:

  1. 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'));

要么:

  1. 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相关文章