因为一个任务,最近几天一直在研究Webbrowser的相关功能,下面是收集到的一些方法
//根据URL获取请求Header
function GetAllHeaders(URL: string): string;
var
hSession: HInternet;
hURL: HInternet;
hReq: HInternet;
bRet: Boolean;
sBuff: String;
lBuffLen:DWord;
dwParam: DWord;
aResult: String;
aServer: string;
aFilePath: string;
I: Integer;
begin
Result := 'Unknown error';
aFilePath := URL;
if pos('http://', aFilePath) = 1 then
Delete(aFilePath, 1, 7);
aServer := SplitAtToken(aFilePath, '/');
dwParam := 0;
hSession := InternetOpen(
'http generic',
INTERNET_OPEN_TYPE_PRECONFIG, {use IE's setup to conect to the web}
nil, nil, 0);
try
if hSession = nil then
Exit; hURL := InternetConnect(hSession,
PChar(aServer), {the server we wants to conect to}
INTERNET_DEFAULT_HTTP_PORT, nil, nil,
INTERNET_SERVICE_HTTP, 0, 1);
try
if hURL = nil then
Exit; hReq := HttpOpenRequest(hURL,
nil, {nil defaults to the 'GET' verb}
PChar(aFilePath), {actual file on the server}
HTTP_VERSION, nil, nil,
INTERNET_FLAG_RELOAD, 0);
try
if hReq = nil then
Exit;
if not HttpSendRequest(hReq, nil, 0, 0, 0) then
Exit;
SetLength(sBuff, 1024); //set plenty of room in buffer
lBuffLen := Length(sBuff); bRet := HttpQueryInfo(hReq,
HTTP_QUERY_RAW_HEADERS_CRLF, {get all headers in one move}
@sBuff[1], lBuffLen, dwParam);
if bRet then
Result := sBuff
else
Result := 'QueryInfo error = '+IntToStr(GetLastError);
finally
InternetCloseHandle(hReq);
end;
finally
InternetCloseHandle(hURL);
end;
finally
InternetCloseHandle(hSession);
end;
end;
//获取 WebBrowser 控件当前显示页面的Html
function GetCompleteHTMLSource(wb: TWebBrowser) : string;
var
iDoc: IHTMLDocument2;
iall : IHTMLElement;
begin
Result := '';
if Assigned(wb.Document) then begin iDoc := wb.Document as IHTMLDocument2;
iall := iDoc.body; while iall.parentElement <> nil do begin
iall := iall.parentElement;
end; Result := iall.outerHTML;
end;
end;