• delphi XE8 NetHTTPRequest NetHTTPClient


    delphi xe8 推出2个新http控件,NetHTTPRequest、NetHTTPClient

    可以调用ASP.Net 一般应用程序获取网页数据,用旧的控件idhttp控件也可以,推荐用新的这2个组件。

    http://localhost:52384/Handler1.ashx

    procedure TForm5.Button1Click(Sender: TObject);
    var
      ss: TStringStream;
    begin
      ss := TStringStream.Create('', TEncoding.ASCII);//中文用UTF8
    ss.Clear; self.NetHTTPClient1.Get(url, ss); self.Caption := ss.DataString; ss.Free; end;
    procedure TForm5.Button2Click(Sender: TObject);
    var
      ss: TStringStream;
    begin
      ss := TStringStream.Create('', TEncoding.ASCII);
      ss.Clear;
      self.NetHTTPRequest1.Get(url);
      self.Caption := ss.DataString;
      ss.Free;
    end;

    http://docwiki.embarcadero.com/RADStudio/XE8/en/What's_New_in_Delphi_and_C%2B%2BBuilder_XE8

     New HTTP Client API

    The RTL provides several new network units with members that let you create an HTTP client, send requests to HTTP servers and handle their responses.

    Using an HTTP Client explains how to use the two new HTTP components, TNetHTTPClient and TNetHTTPRequest, to handle HTTP requests. See also the main HTTP client classes:

    • THTTPClient, that represents an HTTP client.
    • THTTPRequest, that represents a request from your client to a server.
    • THTTPResponse, that represents the server response to a request from your client. 

    THTTPClient

    http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Net.HttpClient.THTTPClient

    OnReceiveData

    example code

    http://docwiki.embarcadero.com/CodeExamples/Berlin/en/RTL.HttpAsyncDownload_Sample

    self.NetHTTPClient1.Get('www.sohu.com').ContentAsString(TEncoding.UTF8);

    NetHTTPClient

    var
      url: string;
      ss: tstringstream;
    begin
      url := Edit1.Text;
    
      ss := tstringstream.Create('', tencoding.UTF8);
      ss.WriteString(Memo1.Text);
      self.NetHTTPClient1.Accept := 'text/html, application/xhtml+xml, */*';
      self.NetHTTPClient1.ContentType := 'application/x-www-form-urlencoded';//application/json
      self.NetHTTPClient1.AcceptEncoding := 'UTF-8';
      ss.Position := 0;
      Memo2.Text := self.NetHTTPClient1.Post(url, ss).ContentAsString(tencoding.UTF8);
      
      ss.Free;
    end;

     IdHTTP

       self.IdHTTP1.Request.ContentType:='application/x-www-form-urlencoded';
       self.IdHTTP1.Request.Accept:= 'text/html, application/xhtml+xml, */*';//application/json
       self.Memo2.Text:=self.IdHTTP1.Post(url,ss);

     NetHTTPRequest

      self.NetHTTPRequest1.Accept := 'text/html, application/xhtml+xml, */*';
      self.NetHTTPRequest1.url := self.Edit1.Text;
      self.NetHTTPRequest1.MethodString := 'POST';
      self.Memo2.Text := self.NetHTTPRequest1.Post(url, ss).ContentAsString(tencoding.UTF8);

     post 多个参数

        TStringList *ss;
        ss = new TStringList();
        ss->Add("PatientID=aaaaaaaaa");
        ss->Add("PictuerName=BBBBBBBBBBB");

        this->Caption = this->NetHTTPClient1->Post("http://127.0.0.1:8080/datasnap/rest/TServerMethods1/UpPic/", ss)->ContentAsString(TEncoding::ASCII);
        delete ss;

  • 相关阅读:
    幂等性-接口安全性
    spring 事务
    Disruptor 并发框架
    java中锁的应用
    线程池原理
    并发队列阻塞式与非阻塞式的区别
    Swagger UI教程 API 文档神器 搭配Node使用
    linux ssh_config和sshd_config配置文件
    Linux中iptables设置详细
    Linux(Centos)之安装Redis及注意事项
  • 原文地址:https://www.cnblogs.com/cb168/p/5297127.html
Copyright © 2020-2023  润新知