• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphiXE8NetHTTPRequestNetHTTPClient

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

TNetHttpClient不依赖OPENSSL

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;


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphi文件搜索,遍历所有子目录发布时间:2022-07-18
下一篇:
Delphi与DirectX之DelphiX(80):TDIB.BlendPixel();发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap