uses Winapi.Wininet function WebPagePost(sURL,sPostData:AnsiString):AnsiString;stdcall; const RequestMethod = 'POST'; HTTP_VERSION = 'HTTP/1.1'; //HTTP版本 我抓包看过 HTTP/1.0 HTTP/1.1。尚未仔细了解其区别。按MSDN来写的。留空默认是1.0 var HttpsFlag:Boolean; dwSize:DWORD; dwFileSize: Int64; dwBytesRead,dwReserved:DWORD; hInte,hConnection,hRequest:HInternet; ContentSize:array[1..1024] of Char; HostPort:Integer; HostName,FileName,sHeader:String; Contents:PAnsiChar; procedure ParseURL(URL: string;var HostName,FileName:string;var HostPort:Integer;var httpsFlag:Boolean); var i,p,k: DWORD; function StrToIntDef(const S: string; Default: Integer): Integer; var E: Integer; begin Val(S, Result, E); if E <> 0 then Result := Default; end; begin if lstrcmpi('http://',PChar(Copy(URL,1,7))) = 0 then System.Delete(URL, 1, 7); if lstrcmpi('https://',PChar(Copy(URL,1,8))) = 0 then begin System.Delete(URL, 1, 8); HttpsFlag:=True; end else HttpsFlag:=False; HostName := URL; FileName := '/'; if HttpsFlag then HostPort := INTERNET_DEFAULT_HTTPS_PORT else HostPort := INTERNET_DEFAULT_HTTP_PORT; i := Pos('/', URL); if i <> 0 then begin HostName := Copy(URL, 1, i-1); FileName := Copy(URL, i, Length(URL) - i + 1); end; p:=pos(':',HostName); if p <> 0 then begin k:=Length(HostName)-p; if HttpsFlag then HostPort:=StrToIntDef(Copy(HostName,p+1,k),INTERNET_DEFAULT_HTTPS_PORT) else HostPort:=StrToIntDef(Copy(HostName,p+1,k),INTERNET_DEFAULT_HTTP_PORT); Delete(HostName,p,k+1); end; end; begin Result := ''; dwFileSize :=0; ParseURL(sURL,HostName,FileName,HostPort,HttpsFlag); // 函数原型见 http://technet.microsoft.com/zh-cn/subscriptions/aa385096(v=vs.85).aspx hInte := InternetOpen('', //UserAgent INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0); if hInte<>nil then begin hConnection := InternetConnect(hInte, // 函数原型见 http://technet.microsoft.com/zh-cn/query/ms909418 PChar(HostName), HostPort, nil, nil, INTERNET_SERVICE_HTTP, 0, 0); if hConnection<>nil then begin if HttpsFlag then 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 or INTERNET_FLAG_SECURE, 0) else 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, 0); if hRequest<>nil then begin sHeader := 'Content-Type: application/x-www-form-urlencoded' + #13#10; // +'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,0,PChar(sPostData),Length(sPostData)) then begin dwReserved:=0; 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(ContentSize); GetMem(Contents, dwFileSize); InternetReadFile(hRequest,Contents,dwFileSize,dwBytesRead); Result:=StrPas(Contents); FreeMem(Contents); end; end; end; InternetCloseHandle(hRequest); end; InternetCloseHandle(hConnection); end; InternetCloseHandle(hInte); end; function WebPageGet(sURL:AnsiString):AnsiString;stdcall; const RequestMethod = 'GET'; HTTP_VERSION = 'HTTP/1.1'; //HTTP版本 我抓包看过 HTTP/1.0 HTTP/1.1。尚未仔细了解其区别。按MSDN来写的。留空默认是1.0 var HttpsFlag:Boolean; dwSize:DWORD; dwFileSize: Int64; dwBytesRead,dwReserved:DWORD; hInte,hConnection,hRequest:HInternet; ContentSize:array[1..1024] of Char; HostPort:Integer; HostName,FileName,sHeader:String; Contents:PAnsiChar; procedure ParseURL(URL: string;var HostName,FileName:string;var HostPort:Integer;var httpsFlag:Boolean); var i,p,k: DWORD; function StrToIntDef(const S: string; Default: Integer): Integer; var E: Integer; begin Val(S, Result, E); if E <> 0 then Result := Default; end; begin if lstrcmpi('http://',PChar(Copy(URL,1,7))) = 0 then System.Delete(URL, 1, 7); if lstrcmpi('https://',PChar(Copy(URL,1,8))) = 0 then begin System.Delete(URL, 1, 8); HttpsFlag:=True; end else HttpsFlag:=False; HostName := URL; FileName := '/'; if HttpsFlag then HostPort := INTERNET_DEFAULT_HTTPS_PORT else HostPort := INTERNET_DEFAULT_HTTP_PORT; i := Pos('/', URL); if i <> 0 then begin HostName := Copy(URL, 1, i-1); FileName := Copy(URL, i, Length(URL) - i + 1); end; p:=pos(':',HostName); if p <> 0 then begin k:=Length(HostName)-p; if HttpsFlag then HostPort:=StrToIntDef(Copy(HostName,p+1,k),INTERNET_DEFAULT_HTTPS_PORT) else HostPort:=StrToIntDef(Copy(HostName,p+1,k),INTERNET_DEFAULT_HTTP_PORT); Delete(HostName,p,k+1); end; end; begin Result := ''; dwFileSize :=0; ParseURL(sURL,HostName,FileName,HostPort,HttpsFlag); // 函数原型见 http://technet.microsoft.com/zh-cn/subscriptions/aa385096(v=vs.85).aspx hInte := InternetOpen('', //UserAgent INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0); if hInte<>nil then begin hConnection := InternetConnect(hInte, // 函数原型见 http://technet.microsoft.com/zh-cn/query/ms909418 PChar(HostName), HostPort, nil, nil, INTERNET_SERVICE_HTTP, 0, 0); if hConnection<>nil then begin if HttpsFlag then 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 or INTERNET_FLAG_SECURE, 0) else 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, 0); if hRequest<>nil then begin sHeader := 'Content-Type: application/x-www-form-urlencoded' + #13#10; // +'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,0,nil,0) then begin dwReserved:=0; 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(ContentSize); GetMem(Contents, dwFileSize); InternetReadFile(hRequest,Contents,dwFileSize,dwBytesRead); Result:=StrPas(Contents); FreeMem(Contents); end; end; end; InternetCloseHandle(hRequest); end; InternetCloseHandle(hConnection); end; InternetCloseHandle(hInte); end;
初步判断了一下https情况,还有flags需要再细致一点设置,还有自定义表头,https的用户名密码等用参数形式封装.
还有一个最主要的问题就是超时问题的解决。这个等以后有时间再弄