关于IdHTTP的一个问题。请大家帮看一下!谢谢.

时间:2023-01-29 20:39:35
function TForm1.Gettoken:string;
var
Aurl:string;
ResponseStream: TStringStream;  //返回信息
RequestList : TStringList;     //请求信息
begin
try
IdHTTP1.Request.ContentType :='text/xml';
Aurl:='http://webapi.cninfo.com.cn/api-cloud-platform/oauth2/token';
ResponseStream := TStringStream.Create('');
RequestList := TStringList.Create;
RequestList.Add('grant_type=client_credentials');
RequestList.Add('client_id=010023');
RequestList.Add('client_secret=1230456');
IdHTTP1.Post(Aurl,RequestList,ResponseStream); //执行到这里会出现异常"HTTP/1.0 999 OK".
RESULT:=ResponseStream.DataString;
finally
  freeandnil(ResponseStream);
  freeandnil(RequestList);
end;
end;

我用的是Delphi2010
请各位帮看一下是什么问题!异常"HTTP/1.0 999 OK"是什么意思?谢谢!

1 个解决方案

#1


加入传输压缩组件: TIdCompressorZLib 应该就可以了。



procedure TForm1.TestClick(Sender: TObject);
var
  Aurl:string;
  zip: TIdCompressorZLib;
  ResponseStream: TStringStream;  //返回信息
  RequestList : TStrings;     //请求信息
begin
  try
    zip := TIdCompressorZLib.Create(IdHTTP1);
    IdHTTP1.Compressor := zip;
    IdHTTP1.Request.ContentType :='application/x-www-form-urlencoded';
    IdHTTP1.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/61.0';
    Aurl:='http://webapi.cninfo.com.cn/api-cloud-platform/oauth2/token';
    ResponseStream := TStringStream.Create('',TEncoding.UTF8);
    RequestList := TStringList.Create;
    RequestList.Add('grant_type=client_credentials');
    RequestList.Add('client_id=010023');
    RequestList.Add('client_secret=1230456');
    IdHTTP1.Post(Aurl,RequestList,ResponseStream); //执行到这里会出现异常"HTTP/1.0 999 OK".
    ShowMessage(ResponseStream.DataString);
  finally
    freeandnil(ResponseStream);
    freeandnil(RequestList);
    freeandnil(zip);
  end;
end;


测试了下,返回如下:
{"error":"invalid_client","error_description":"客户端验证失败,如错误的client_id/client_secret。"}

#1


加入传输压缩组件: TIdCompressorZLib 应该就可以了。



procedure TForm1.TestClick(Sender: TObject);
var
  Aurl:string;
  zip: TIdCompressorZLib;
  ResponseStream: TStringStream;  //返回信息
  RequestList : TStrings;     //请求信息
begin
  try
    zip := TIdCompressorZLib.Create(IdHTTP1);
    IdHTTP1.Compressor := zip;
    IdHTTP1.Request.ContentType :='application/x-www-form-urlencoded';
    IdHTTP1.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/61.0';
    Aurl:='http://webapi.cninfo.com.cn/api-cloud-platform/oauth2/token';
    ResponseStream := TStringStream.Create('',TEncoding.UTF8);
    RequestList := TStringList.Create;
    RequestList.Add('grant_type=client_credentials');
    RequestList.Add('client_id=010023');
    RequestList.Add('client_secret=1230456');
    IdHTTP1.Post(Aurl,RequestList,ResponseStream); //执行到这里会出现异常"HTTP/1.0 999 OK".
    ShowMessage(ResponseStream.DataString);
  finally
    freeandnil(ResponseStream);
    freeandnil(RequestList);
    freeandnil(zip);
  end;
end;


测试了下,返回如下:
{"error":"invalid_client","error_description":"客户端验证失败,如错误的client_id/client_secret。"}