最近開發需要向網站進行數據抓包等,先用webbrowser請求網頁,獲取元素取值后再用idhttp向網頁進行get操作。因為涉及到驗簽、登錄等,一直不成功,后來用網絡數據抓包工具發現在用webbrowser和IDhttp請求時數據相同,只是cookie不同。
1、數據抓包設置cookie: Set-Cookie: JsessionID=4nLLYm1XrVtMWJL5VvG2y13RyXJbvhv2kth3SJ5hGpbL7Jv4ZTQk!1183038173!NONE; path=/; HttpOnly
2、webbrowser請求時的Cookie: JSESSIONID=QTnTYq6QyCvCBdyhtBRLP4K4WLzxGQ2LCSbDSNBF8zKDPqt1WMDJ!1003383531!-543092864; http:xxxxxxx=3071348908.44105.0000
3、idhttp請求時的Cookie: http:xxxxxxx=3071348908.44105.0000。
原來(CookieWEB.document as ihtmldocument2).cookie是無法獲取webbrowser的所有cookie的,需要借助"'wininet.dll"才能獲取完整的cookie,然后賦值給idhttp就可以成功了。
function GetCookie(url: string): string;const INTERNET_COOKIE_HTTPONLY = $00002000; INTERNET_COOKIE_THIRD_PARTY = $00000010; INTERNET_FLAG_RESTRICTED_ZONE= $00020000;var hModule:THandle; InternetGetCookieEx:function(lpszUrl, lpszCookieName,lpszCookieData: PChar; var lpdwSize: DWord;dwFlags:DWORD;lPReserved: Pointer): BOOL;StdCall; CookieSize:DWORD; cookiedata:PWideChar; thebool:bool;begin result := ''; hModule:=GetModuleHandle('wininet.dll'); if hModule<>0 then begin @InternetGetCookieEx:=GetProcAddress(hModule,'InternetGetCookieExW'); if @InternetGetCookieEx<>nil then begin CookieSize:=10240; Cookiedata := AllocMem(CookieSize); thebool:=InternetGetCookieEx(PWideChar(url),nil,CookieData,CookieSize,INTERNET_COOKIE_HTTPONLY,nil); if thebool then result := CookieData; FreeMem(Cookiedata); end; FreeLibrary(hModule); end;end;
新聞熱點
疑難解答
圖片精選