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两个文件

最新文章

  1. 基于ruby的watir自动化测试 笔记一
  2. java 大数计算
  3. WCDMA是什么意思?CDMA是什么意思?GSM是什么意思
  4. powershell玩转litedb数据库
  5. 【freemaker】之Unknown built-in variable: now
  6. .NET设计模式(13):享元模式(Flyweight Pattern)(转)
  7. MySQL之改_update
  8. 解决arcgis javascript textsymbol不支持多行文本标注的问题
  9. Javsssist用InsertAt()方法对语句插桩
  10. 管道模式——pipeline与valve
  11. Maven json-lib依赖下载不下来解决方案
  12. C# 虹软离线SDK引擎 人脸识别
  13. centos6.5下vsftpd服务的安装及配置并通过pam认证实现虚拟用户文件共享
  14. 一个有趣的nginx问题引发的小问题
  15. C#基础-代码部署数据库及IIS站点
  16. DBA面试题及解答
  17. leetcode-137-Single Number II-第二种解法
  18. 【Docker 命令】- login 命令
  19. iOS 错误提示
  20. cURL使用教程及实例演示

热门文章

  1. Web前端的状态管理(State Management)
  2. Windows7 64位安装最新版本号MySQLserver
  3. poj Ping pong LA 4329 (树状数组统计数目)
  4. 0046 @Transactional注解的几个参数--事务传播控制--事务隔离级别--异常与回滚
  5. List、Set、Map集合大杂烩
  6. uinty实现玩家尾随鼠标位置平滑旋转角度
  7. splay旋转模板
  8. 用阿里云搭建Http代理服务器
  9. SQL宝典
  10. javascript屏蔽浏览器右键功能按钮