Home > Back-end >  Using webbrowser login access cookies, then why still use idhttp to get the login information before
Using webbrowser login access cookies, then why still use idhttp to get the login information before

Time:09-25

Solving using webbrowser login access cookies, then why still use idhttp to get the login information before
The code is as follows:
IDhttp2:=TIdHTTP. Create (nil);
IDhttp2. ReadTimeout:=20000;//timeout
IDHttp2. ConnectTimeout:=10000;
IDhttp2. AllowCookies:=false;//custom management cookies
IDhttp2. Request. ContentType:='application/x - WWW - form - urlencoded; Charset=utf-8 ';
IDhttp2. Request. UserAgent:='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) ';
IDhttp2. Request. Accept:='Accept=*/*';
IDhttp2. Request. AcceptCharSet:='gb2312, utf-8; Q=0.7 *; Q=0.7 ';
IDhttp2. Request. Referer:='https://kyfw.12306.cn/otn/login/init';
IDhttp2. Request. AcceptLanguage:='useful - cn;
IDhttp2. HandleRedirects:=True;//support redirect
IDhttp2. Request. CacheControl:='no - cache;//no cache
IDhttp2. Request. AcceptEncoding:='identity';//'gzip, deflatee'; Compressed format, if you don't need, just empty
IDhttp2. Request. Host:='cn' kyfw. 12306.//host url
IDhttp2. HTTPOptions:=IDhttp2. HTTPOptions + [hoKeepOrigProtocol];//set protocol
IDhttp2. ProtocolVersion:=pv1_1;
IDhttp2. Request. Connection:='Keep - the Alive;
IDHttp2. IOHandler:=Form2. IDsl1;
URL:='http://bbs.csdn.net/home';
WEBCooKies:=(wB1. Document as ihtmldocument2). Cookies;
CookieStr:='cookies: + WEBCooKies;
Form2. Mmo2. Text:=CookieStr;
IdHttp2. Request. CustomHeaders. Text:=CookieStr;
Form2. Mmo1. Text:=UTF8Decode (IDHttp2. Get (URL));

CodePudding user response:

Problem on obtaining webBrowser cookies, so take does not take to all of the cookie information, you can take to their cookies displayed and caught under the contrast,
Access to the browser cookies can use this function:

The 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;
The begin
Result:=';
HModule:=GetModuleHandle (' wininet. DLL);
If hModule<> 0 then
The begin
@ InternetGetCookieEx:=GetProcAddress call (hModule, 'InternetGetCookieExW');
If @ InternetGetCookieEx<> Nil then
The 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;

CodePudding user response:

refer to the original poster kfuuuupl8 response:
solution using webbrowser land access to cookies, then why still use idhttp to get the login information before
The code is as follows:
IDhttp2:=TIdHTTP. Create (nil);
IDhttp2. ReadTimeout:=20000;//timeout
IDHttp2. ConnectTimeout:=10000;
IDhttp2. AllowCookies:=false;//custom management cookies
IDhttp2. Request. ContentType:='application/x - WWW - form - urlencoded; Charset=utf-8 ';
IDhttp2. Request. UserAgent:='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) ';
IDhttp2. Request. Accept:='Accept=*/*';
IDhttp2. Request. AcceptCharSet:='gb2312, utf-8; Q=0.7 *; Q=0.7 ';
IDhttp2. Request. Referer:='https://kyfw.12306.cn/otn/login/init';
IDhttp2. Request. AcceptLanguage:='useful - cn;
IDhttp2. HandleRedirects:=True;//support redirect
IDhttp2. Request. CacheControl:='no - cache;//no cache
IDhttp2. Request. AcceptEncoding:='identity';//'gzip, deflatee'; Compressed format, if you don't need, just empty
IDhttp2. Request. Host:='cn' kyfw. 12306.//host url
IDhttp2. HTTPOptions:=IDhttp2. HTTPOptions + [hoKeepOrigProtocol];//set protocol
IDhttp2. ProtocolVersion:=pv1_1;
IDhttp2. Request. Connection:='Keep - the Alive;
IDHttp2. IOHandler:=Form2. IDsl1;
URL:='http://bbs.csdn.net/home';
WEBCooKies:=(wB1. Document as ihtmldocument2). Cookies;
CookieStr:='cookies: + WEBCooKies;
Form2. Mmo2. Text:=CookieStr;
IdHttp2. Request. CustomHeaders. Text:=CookieStr;
Form2. Mmo1. Text:=UTF8Decode (IDHttp2. Get (URL));

Hello: have you this way I can not get COOKIE, is what happened

CodePudding user response:

reference 1st floor lynmison response:
problem on obtaining webBrowser cookies, so take does not take to all of the cookie information, you can take to their cookies displayed and caught under the contrast,
Access to the browser cookies can use this function:

The 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;
The begin
Result:=';
HModule:=GetModuleHandle (' wininet. DLL);
If hModule<> 0 then
The begin
@ InternetGetCookieEx:=GetProcAddress call (hModule, 'InternetGetCookieExW');
If @ InternetGetCookieEx<> Nil then
The begin
CookieSize:=10240;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related