delphi7中idHTTP的使用

1.直接idhttp:Tidhttp声明一个var变量是不能使用的。必须有控件

2.使用IdHTTP调post接口,xml格式

        IdHTTP1.HandleRedirects := True;//允许头转向
        IdHTTP1.ReadTimeout := 30000;//请求超时设置
        IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions+[hoKeepOrigProtocol];
        IdHTTP1.ProtocolVersion:=pv1_1;
        IdHTTP1.Request.Connection := 'keep-alive';
        IdHTTP1.Request.CacheControl := 'no-cache';
        IdHTTP1.Request.AcceptLanguage:='zh-cn';
        IdHTTP1.Request.AcceptEncoding:='gzip, deflate';
        IdHTTP1.Request.ContentType := 'text/xml;application/x-www-form-urlencoded; Charset=UTF-8';  
        
        postStream:=TStringStream.Create(ansitoUTF8(gv_mz_cf_xml));

        responseStr:=IdHTTP1.Post(gv_hlyy_post_url,postStream);

可以post成功

注意点:a. var postStream:TStringStream

postStream:=TStringStream.Create(ansitoUTF8(gv_mz_cf_xml));

如果用Tstringlist,接受不到参数,改成TStringStream时,可以接受到数据,但是编码后的

b.解决编码后的参数

  IdHTTP1.Request.ContentType := 'text/xml;application/x-www-form-urlencoded; Charset=UTF-8';  

必须用text/xml

如果是json,使用text/json

你可能感兴趣的:(delphi学习)