delphi 模拟POST提交数据
unit GetHttpInfo; interface uses Classes, WinINet, Sysutils, windows, IDURI, IdSSLOpenSSL
, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP; procedure Get(url: string; res: TStream); overload;
procedure Post(url, data: string; res: TStream); overload;
function Get(url: string): string; overload;
function WebPagePost(sURL, sPostData: string): Pointer; stdcall;
function WinPost(url, data: string):string; implementation function Get(url: string): string;
var
s: TStringStream;
begin
s := TStringStream.Create('');
try
Get(url, s); result := UTF8ToAnsi(s.DataString);
finally
s.Free;
end;
end; procedure Post(url, data: string; res: TStream);
var
hInt, hConn, hreq: HINTERNET;
buffer: PChar;
dwRead, dwFlags: cardinal;
port: Word;
uri: TIdURI;
proto, host, path, Params: string;
begin
uri := TIdURI.Create(url);
host := uri.Host;
path := uri.Path + uri.Document;
proto := uri.Protocol;
Params := uri.Params;
uri.Free;
if UpperCase(proto) = 'HTTPS' then
begin
port := INTERNET_DEFAULT_HTTPS_PORT;
dwFlags := INTERNET_FLAG_SECURE or INTERNET_FLAG_RELOAD;
end
else
begin
port := INTERNET_INVALID_PORT_NUMBER;
dwFlags := INTERNET_FLAG_RELOAD;
end;
hInt := InternetOpen('Delphi', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, );
hConn := InternetConnect(hInt, PChar(host), port, nil, nil, INTERNET_SERVICE_HTTP, , );
hreq := HttpOpenRequest(hConn, 'POST', PChar(Path + Params), 'HTTP/1.1', nil, nil, dwFlags, );
GetMem(buffer, );
data := AnsiToUTF8(data);
//if HttpSendRequestEx(hReq, nil, 0, PChar(data), Length(data) then
//if HttpSendRequestA(hReq, nil, 0, PChar(data), Length(data)) then
if HttpSendRequest(hReq, nil, , PChar(data), Length(data)) then
begin
dwRead := ;
repeat
InternetReadFile(hreq, buffer, , dwRead);
if dwRead <> then
res.Write(buffer^, dwRead);
until dwRead = ;
end;
InternetCloseHandle(hreq);
InternetCloseHandle(hConn);
InternetCloseHandle(hInt);
FreeMem(buffer);
end; procedure Get(url: string; res: TStream);
var
hInt, hUrl: HINTERNET;
buffer: PChar;
dwRead: cardinal;
begin
GetMem(buffer, ); hInt := InternetOpen('Delphi', INTERNET_OPEN_TYPE_DIRECT, nil, nil, );
dwRead := ;
hurl := InternetOpenUrl(hInt, PChar(url), nil, , INTERNET_FLAG_RELOAD, );
repeat
InternetReadFile(hUrl, buffer, , dwRead);
if dwRead <> then
res.Write(buffer^, dwRead);
until dwRead = ;
InternetCloseHandle(hUrl);
InternetCloseHandle(hInt);
FreeMem(buffer);
end; function WebPagePost(sURL, sPostData: string): Pointer; stdcall;
const
RequestMethod = 'POST';
HTTP_VERSION = 'HTTP/1.1'; //HTTP版本 我抓包看过 HTTP/1.0 HTTP/1.1。尚未仔细了解其区别。按MSDN来写的。留空默认是1.0
var
dwSize: DWORD;
dwFileSize: Int64;
dwBytesRead, dwReserved: DWORD;
hInte, hConnection, hRequest: HInternet;
ContentSize: array[..] of Char;
HostPort: Integer;
HostName, FileName, sHeader: string;
procedure ParseURL(URL: string; var HostName, FileName: string; var HostPort: Integer);
var
i, p, k: DWORD;
function StrToIntDef(const S: string; Default: Integer): Integer;
var
E: Integer;
begin
Val(S, Result, E);
if E <> then Result := Default;
end;
begin
if lstrcmpi('http://', PChar(Copy(URL, , ))) = then System.Delete(URL, , );
HostName := URL;
FileName := '/';
HostPort := INTERNET_DEFAULT_HTTP_PORT;
i := Pos('/', URL);
if i <> then
begin
HostName := Copy(URL, , i - );
FileName := Copy(URL, i, Length(URL) - i + );
end;
p := pos(':', HostName);
if p <> then
begin
k := Length(HostName) - p;
HostPort := StrToIntDef(Copy(HostName, p + , k), INTERNET_DEFAULT_HTTP_PORT);
Delete(HostName, p, k + );
end;
end;
begin
Result := Pointer(-);
dwFileSize := ;
ParseURL(sURL, HostName, FileName, HostPort);
// 函数原型见 http://technet.microsoft.com/zh-cn/subscriptions/aa385096(v=vs.85).aspx
hInte := InternetOpen('', //UserAgent
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, );
if hInte <> nil then
begin
hConnection := InternetConnect(hInte, // 函数原型见 http://technet.microsoft.com/zh-cn/query/ms909418
PChar(HostName),
HostPort,
nil,
nil,
INTERNET_SERVICE_HTTP,
,
);
if hConnection <> nil then
begin
hRequest := HttpOpenRequest(hConnection, // 函数原型见 http://msdn.microsoft.com/zh-cn/library/aa917871
PChar(RequestMethod),
PChar(FileName),
HTTP_VERSION,
'', //Referrer 来路
nil, //AcceptTypes 接受的文件类型 TEXT/HTML */*
INTERNET_FLAG_NO_CACHE_WRITE or
INTERNET_FLAG_RELOAD,
);
if hRequest <> nil then
begin
sHeader := 'Content-Type: application/x-www-form-urlencoded' + ##;
// +'CLIENT-IP: 216.13.23.33'+#13#10
// 'X-FORWARDED-FOR: 216.13.23.33' + #13#10+; 伪造代理IP // 函数原型见 http://msdn.microsoft.com/zh-cn/library/aa384227(v=VS.85)
HttpAddRequestHeaders(hRequest, PChar(sHeader),
Length(sHeader),
HTTP_ADDREQ_FLAG_ADD or HTTP_ADDREQ_FLAG_REPLACE);
// 函数原型见 http://msdn.microsoft.com/zh-cn/library/windows/desktop/aa384247(v=vs.85).aspx
if HttpSendRequest(hRequest, nil, , PChar(sPostData), Length(sPostData)) then
begin
dwReserved := ;
dwSize := SizeOf(ContentSize);
// 函数原型 http://msdn.microsoft.com/zh-cn/subscriptions/downloads/aa384238.aspx
if HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH, @ContentSize, dwSize, dwReserved) then
begin
dwFileSize := StrToInt(StrPas(@ContentSize));
GetMem(Result, dwFileSize);
InternetReadFile(hRequest, Result, dwFileSize, dwBytesRead);
end;
end;
end;
InternetCloseHandle(hRequest);
end;
InternetCloseHandle(hConnection);
end;
InternetCloseHandle(hInte);
end; function WinPost(url, data:string):string;
var
IdHttp : TIdHTTP;
ResponseStream : TStringStream; //返回信息
ResponseStr : string;
idSSL : TIdSSLIOHandlerSocket;
RequestList : TStringList; //请求信息
RequestStream : TStringStream;
begin
//创建IDHTTP控件
IdHttp := TIdHTTP.Create(nil);
idSSL := TIdSSLIOHandlerSocket.Create(nil);
//TStringStream对象用于保存响应信息
//res := TStringStream.Create('');
ResponseStream := TStringStream.Create('');
RequestStream := TStringStream.Create('');
RequestList := TStringList.Create;
try
try
idSSL.SSLOptions.Method:= sslvSSLv3;
IdHttp.IOHandler:= idSSL;
//以列表的方式提交参数
//RequestList.Add('text=love');
//IdHttp.Post(Url,RequestList,res);
IdHttp.Request.ContentType :='application/json';
//以流的方式提交参数
RequestStream.WriteString(data);
IdHttp.Post(Url,RequestStream,ResponseStream);
except end; //获取网页返回的信息
ResponseStr := ResponseStream.DataString;
result:= ResponseStr;
//网页中的存在中文时,需要进行UTF8解码
// ResponseStr := UTF8Decode(ResponseStr);
finally
IdHttp.Free;
RequestList.Free;
RequestStream.Free; end;
end; end.
使用WinPost提交数据在xp下没有问题,Post方法在win7以上没有问题.
WinPost只支持https提交,其它方法支持https和http
使用WinPost方法需要使用dll文件libeay32.dll、ssleay32.dll两个文件
delphi 模拟POST提交数据的更多相关文章
- cURL模拟POST提交数据
首先,是这个代码: <?php //curl模拟post提交数据$url = "http://127.0.0.1/immoc/output.php";$post_data = ...
- 使用PHP模拟post提交数据
使用PHP模拟post提交数据 分类: PHP LAMP 2013-04-13 12:03 3954人阅读 评论(0) 收藏 举报 CurlsocketPHP 这也是个老生常谈的话题了,上午花了点时间 ...
- Fiddler进行模拟Post提交数据,总为null解决方式
Fiddler模拟post提交时总是为空,解决办法 如果是表单提交则要在header加上 ContentType:application/x-www-form-urlencoded 如果是要post提 ...
- php CURL 模拟 POST 提交数据
<?php function liansuo_post($url,$data){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 curl_seto ...
- php模拟post提交数据,用处很多,可用来网站的采集,登陆等等
1. [代码][PHP]代码 <?php //以程序登陆一个论坛登录为例 function bbslogin($user_login, $password, $host, $port = &qu ...
- 三种方法教你如何用PHP模拟post提交数据
php模拟post传值在日常的工作中用到的不是很多,但是在某些特定的场合还是经常用到的. 下面,我整理了三种php模拟post传值的方法,file_get_contents.curl和socket. ...
- 模拟form提交数据
最近在做一个项目,发现ajax不能enctype=”multipart/form-data” 属性的表单,没办法,只能使用form表单直接提交的方法了,但是form表单直接提交会跳转页面,这样很不友好 ...
- Asp.Net模拟post提交数据方法
方法1: System.Net.WebClient WebClientObj = new System.Net.WebClient(); System.Collections.Specialized. ...
- php模拟post提交数据
$data = '{ "id": "17999030", "method": "sayHello", "jso ...
随机推荐
- php-fpm配置详解
php-fpm详解 原文链接:http://php-fpm.anight.org/wiki:http://www.php-fpm.com/翻译:http://syre.blogbus.com/logs ...
- 将json数组字符串转换为json数组对象(值是json对象的数组)
var str1 ='[{"name":"kevin","age":18},{"name":"rose&quo ...
- CCNA2.0笔记_子网划分
http://files.cnblogs.com/files/airoot/%E5%AD%90%E7%BD%91%E5%88%92%E5%88%86.zip 网络 默认子网掩码 A类 255.0.0. ...
- oracle表空间中PCTFREE, PCTUSED, INITRANS, MAXTRANX参数的解释
1. PCTFREE 要形容一个 BLOCK 的运作,我们可以把一个 BLOCK 想成一个水杯.侍者把水倒入放在我们面前的水杯,要多满呢,我们要求他倒 9 分满好了,这时候 PCTFREE 代表着设定 ...
- iOS崩溃解决记录
Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contai ...
- IPC之SystemV
svipc - System V interprocess communication mechanisms linux实现的System V interprocess communication ( ...
- Squares - poj 2002(hash)
枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点. #include<stdio.h> #include<string.h> #include<s ...
- linux 启动jar命令
进入ECS目录jar 存放目录: 1.执行命令: linux:启动jar nohup java -jar qualityshop-api.jar>log.file 2>&1 &am ...
- Centos 下面安装 docker
试过了虚拟机VM ,今天尝试在虚拟机centos 中安装 docker ,入门是看的阮一峰的http://www.ruanyifeng.com/blog/2018/02/docker-tutorial ...
- Ubuntu apt-get update错误解决
用apt-get命令安装开发软件非常方便,但由于各种原因,经常链接不上软件源,于是需要使用sudo apt-get update命令来更新软件源. 而屋漏偏逢连夜雨,这时候更新命令也罢工,出现各种错误 ...