淘先锋技术网

首页 1 2 3 4 5 6 7

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;