Delphi IDHTTP控件:GET/POST 请求
Delphi IDHTTP控件:GET/POST 请求
最近一直在使用IDHTTP,下面是一些关于 GET、POST 请求基本使用方法的代码
一、GET 请求

1 procedure GetDemo;
2 var
3 IdHttp : TIdHTTP;
4 Url : string;//请求地址
5 ResponseStream : TStringStream; //返回信息
6 ResponseStr : string;
7 begin
8 //创建IDHTTP控件
9 IdHttp := TIdHTTP.Create(nil);
10 //TStringStream对象用于保存响应信息
11 ResponseStream := TStringStream.Create('');
12 try
13 //请求地址
14 Url := 'http://dict.youdao.com/';
15 try
16 IdHttp.Get(Url,ResponseStream);
17 except
18 on e : Exception do
19 begin
20 ShowMessage(e.Message);
21 end;
22 end;
23 //获取网页返回的信息
24 ResponseStr := ResponseStream.DataString;
25 //网页中的存在中文时,需要进行UTF8解码
26 ResponseStr := UTF8Decode(ResponseStr);
27 finally
28 IdHttp.Free;
29 ResponseStream.Free;
30 end;
31 end;

如果Get需要添加请求参数,则直接在地址后添加,各参数间用&连接
如:http://dict.youdao.com?param1=1¶m2=2
二、Post 请求

1 procedure PostDemo;
2 var
3 IdHttp : TIdHTTP;
4 Url : string;//请求地址
5 ResponseStream : TStringStream; //返回信息
6 ResponseStr : string;
7
8 RequestList : TStringList; //请求信息
9 RequestStream : TStringStream;
10 begin
11 //创建IDHTTP控件
12 IdHttp := TIdHTTP.Create(nil);
13 //TStringStream对象用于保存响应信息
14 ResponseStream := TStringStream.Create('');
15
16 RequestStream := TStringStream.Create('');
17 RequestList := TStringList.Create;
18 try
19 Url := 'http://f.youdao.com/?path=fanyi&vendor=fanyiinput';
20 try
21 //以列表的方式提交参数
22 RequestList.Add('text=love');
23 IdHttp.Post(Url,RequestList,ResponseStream);
24
25 //以流的方式提交参数
26 RequestStream.WriteString('text=love');
27 IdHttp.Post(Url,RequestStream,ResponseStream);
28 except
29 on e : Exception do
30 begin
31 ShowMessage(e.Message);
32 end;
33 end;
34 //获取网页返回的信息
35 ResponseStr := ResponseStream.DataString;
36 //网页中的存在中文时,需要进行UTF8解码
37 ResponseStr := UTF8Decode(ResponseStr);
38 finally
39 IdHttp.Free;
40 RequestList.Free;
41 RequestStream.Free;
42 ResponseStream.Free;
43 end;
44 end;

Post请求在网页中多使用List形式提交参数。
不过在一些API中规定了POST的请求格式为 JSON 格式或 XML,这是需要注意发起请求前需要先设置 ContentType 属性,使用Stream方式提交
已上面代码为例:
提交 JSON 格式:IdHttp.Request.ContentType :='application/json';
提交 XML 格式: IdHttp.Request.ContentType :='text/xml';
如未按要求格式提交,一般会返回 HTTP 1.1 / 415
Delphi IDHTTP控件:GET/POST 请求的更多相关文章
- HTTP 常见异常状态及Delphi IDHTTP 控件处理方式
以下部分为网上查找,部分为工作中整理 200:请求成功 202:请求被接受,但处理尚未完成 302:请求到的资源在一个不同的URL处临时保存 处理方式:重定向到临时的URL(IDHTTP处理方 ...
- Delphi WebBrowser控件的使用(大全 good)
Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application 如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...
- <总结>delphi WebBrowser控件的使用中出现的bug
Delphi WebBrowser控件的使用中出现的bug: 1.WebBrowser.Visible=false:Visible属性不能使WebBrowser控件不可见,暂时用 WebBrowse ...
- 修改Delphi工具控件的默认字体
修改Delphi工具控件的默认字体: 注册表: Delphi 6: HKEY_CURRENT_USER\Software\Borland\Delphi\6.0Delphi 7: HKEY_ ...
- Delphi TcxtreeList控件说明 转
Delphi TcxtreeList控件说明 树.cxTreeList 属性: Align:布局,靠左,靠右,居中等 AlignWithMargins:带边框的布局 Anchors:停靠 (akT ...
- delphi按钮控件的default属性
delphi按钮控件的default属性用于设置默认命令按钮,.设置为true时,按[Enter键]相当于用鼠标单击了该按钮 .窗口中如果有多个按钮的default是true的话,就根据tabinde ...
- Delphi fmx控件在手机滑动与单击的问题
Delphi fmx控件在手机滑动与单击的问题 (2016-03-08 10:52:00) 转载▼ 标签: it delphi 分类: Delphi10 众所周知,fmx制作的app,对于象TEdit ...
- Delphi maskedit控件的掩码含义及用法方法
Delphi maskedit控件的掩码含义及用法方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...
- Delphi StringGrid控件的用法
Delphi StringGrid控件 组件名称:StringGrid ●固定行及固定列: StringGrid.FixedCols:=固定行之数; StringGrid.Fixe ...
随机推荐
- Spring MVC 请求处理方法
以下两种都可以处理用户请求,但请求处理方法值得是第二种 1. SpringMVC 提供的 Controller 接口中公开的 ModelAndView handleRequest(request, r ...
- Increase PHP script execution time with Nginx
If you have a large WordPress setup or a server with limited resources, then you will often see the ...
- sqli-labs:1-4,基于报错的注入
sqli1: 脚本 # -*- coding: utf-8 -*- """ Created on Sat Mar 23 09:37:14 2019 @author: ke ...
- mongoDB(Linux)
启动 service mongod start 安装好后,输入mongo进入控制台 创建数据库 use baseName db.createCollection("game_record& ...
- Angular的一些用法或者结构技巧
如果有更好的方式,请留言交流: 2017-07-07 多个controller共用一个函数.在$rootScope中定义方法, $rootScope.share_fun = function test ...
- Tomcat优化详细1
在Tomcat和应用程序进行了压力测试后,如果您对应用程序的性能结果不太满意,就可以采取一些性能调整措施了,当然了前提是应用程序没有问题,我们这里只讲Tomcat的调整.由于Tomcat的运行依赖于J ...
- 我们用整整三年时间,建成了一套软件:用户定制系统(UD)
这是我们花了三年时间,完成了一套软件--用户定制系统(UD) 主要功能就是集中在下面这个界面了 (自己生成自己哦) ============================= 更多详情,请您访问:我们 ...
- JNI,RegisterNative参数解析
Register native method - 数据类型和method descriptor 使用JNI时,为了使得虚拟机可以找到在C/C++ code中定义的native方法,有两种机制可以用,一 ...
- OpenCV(1):显示图像
显示图像 #include<iostream> #include<opencv2/core/core.hpp> #include<opencv2/highgui/high ...
- Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)
https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...