如何使用Tidhttp使用名为xml的参数发出Get请求? [英] How can I use Tidhttp to make a Get request with a parameter called xml?

查看:191
本文介绍了如何使用Tidhttp使用名为xml的参数发出Get请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功使用Delphi 2010来发出http get请求,但是对于一个需要一个名为'xml'的参数的服务,请求失败并出现'HTTP / 1.1 400错误请求'错误。

I've been successfully using Delphi 2010 to make an http get requests, but for one service that expects a parameter called 'xml' the request fails with a 'HTTP/1.1 400 Bad Request' error.

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

I notice that calling the same service and omitting the 'xml' parameter works.

我尝试过以下操作但没有成功:

I have tried the following with no success:

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'或具有与上述相同值的名称也有效。我也试过了charset的多种组合,但我认为indy组件正在内部更改它。

Calling the same url with the parameter name 'xml' renamed to anything else, like 'xml2' or 'name' with the same value as above also works. I have also tried multiple combinations of the charset, but I think the indy component is changing it internally.

编辑

服务预期:

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

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

Has anyone had any experience with this?

谢谢

推荐答案

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

You need to encode the parameter data when passing it via a URL, TIdHTTP will not encode the URL for you, eg:

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

这篇关于如何使用Tidhttp使用名为xml的参数发出Get请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆