Android Question Share session between Webview and httpclient

rosippc64a

Active Member
Licensed User
Longtime User
Hi All!
There is a webpage, what needs login and password. I have it both. I can view this webpage with webview well. There is a download possibility, but the webview can't download files so I try download by okhttpclient. Since the download link shows another php file, i.e.:
and the download.php checks the login, will be an error, because I am logged in only on webview and not okhttpclient. Because the login datas are stored in PHP session variables, the httpjob.username/password are useless. I tried also set cookies:
B4X:
j.GetRequest.SetHeader("Cookie","[email protected];loginpass=yyyyy")
'or from the main webpage's cookies:   
j.GetRequest.SetHeader("Cookie",CookieManager1.GetCookie("dital.hu"))
I read a lot and found (maybe) solution for this problem here: https://stackoverflow.com/questions/11224454/android-share-session-between-webview-And-httpclient, but I am not a clever man to translate this code into b4a:
B4X:
public static String getCookieFromAppCookieManager(String url) throws MalformedURLException {
    CookieManager cookieManager = CookieManager.getInstance();
    if (cookieManager == null)
        return null;
    String rawCookieHeader = null;
    URL parsedURL = new URL(url);

    // Extract Set-Cookie header value from Android app CookieManager for this URL
    rawCookieHeader = cookieManager.getCookie(parsedURL.getHost());
    if (rawCookieHeader == null)
        return null;
    return rawCookieHeader;
}
, by the way I started, but there was error:
B4X:
#If Java

import java.net.MalformedURLException;
import java.net.CookieManager;
import java.net.CookieHandler;
import java.net.CookiePolicy;
import java.net.URL;

public static String getCookieFromAppCookieManager(String url) throws MalformedURLException {
    CookieManager cookieManager = CookieManager.getInstance();
    if (cookieManager == null)
        return null;
    String rawCookieHeader = null;
    URL parsedURL = new URL(url);

    // Extract Set-Cookie header value from Android app CookieManager for this URL
    rawCookieHeader = cookieManager.getCookie(parsedURL.getHost());
    if (rawCookieHeader == null)
        return null;
    return rawCookieHeader;
}
#End If
maybe since java version mismatch. The error was:
Javac 1.8.0_91
src\b4a\savehtml\main.java:918: error: cannot find symbol
CookieManager cookieManager = CookieManager.getInstance();
^
symbol: method getInstance()
location: class CookieManager
May solve the main problem this approach?
thanks in advance
Steven
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

rosippc64a

Active Member
Licensed User
Longtime User
Thanks for your fast reply, but as you can see at my code fragment,
B4X:
j.GetRequest.SetHeader("Cookie",CookieManager1.GetCookie("dital.hu"))
, I use the cookiemanager library.
 
Upvote 0

rosippc64a

Active Member
Licensed User
Longtime User
I thought maybe the webview's phpsession data will help for okhttpclient to download files.
I would:
1. show webpage -ok
2. log in -ok
3. get the html source -ok
4. process the html source and get the download link(s) -ok
5. get the session cookies from webview - to do
6. start an okhttpclient and set the session cookies and do download file - ok and to do
 
Last edited:
Upvote 0
Top