Authentication header issue in webview - android

I am working on an android application. In my app i have to open some image and pdf from webservice.I could open the image by using following code.
HttpGet httpRequest = new HttpGet(URL);
httpRequest.addHeader("Accepts", "application");
httpRequest.addHeader("User-Authentication",
"c2hpbmVAaWxlYWZzb2x1dGlvbnMuY29tOnNoaW5laWxlYWY=");
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
ANd now I have to open pdf in a webview.So I write the folowing code
WebView mWebView=new WebView(MyPdfViewActivity.this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
HashMap<String, String> map = new HashMap<String, String>();
map.put("Accepts", "application");
map.put("User-Authentication", "c2hpbmVAaWxlYWZzb2x1dGlvbnMuY29tOnNoaW5laWxlYWY=");
mWebView.loadUrl(URL,map);
But I am getting authentication header is not available error. Because of this problem my project is stuck. I find a same type problem here.
check this answer here the accepted answer is "You would need to fetch the page yourself (e.g., via HttpClient) and load it into the WebView that way". How Can I do this?.
Please help me to fix the issue friends.

Look into the documentation.
additionalHttpHeaders
The additional headers to be used in the HTTP request for this URL, specified as a map from name to value. Note that if this map contains any of the headers that are set by default by this WebView, such as those controlling caching, accept types or the User-Agent, their values may be overriden by this WebView's defaults.
Probably your User-Authentication header is also overriden.

Related

Add custom headers coming from android webview [duplicate]

This question already has answers here:
Add custom headers to WebView resource requests - android
(11 answers)
Closed 2 years ago.
I need to add custom headers to EVERY request coming from the WebView. I know loadURL has the parameter for adding extra Headers, but those are only applied to only some of the requests.
All (resource related) requests do not contain the headers.
I have looked at all overrides in WebViewClient, but nothing allows for adding headers to resource requests - onLoadResource(WebView view, String url) and shouldInterceptRequest(Webview,url). Any help would be wonderful.
shouldInterceptRequest(Webview,url) can help you to intercept every request of a site, such as JavaScript, CSS, Image. Then inside shouldInterceptRequest(Webview,url) you can use the parameter url to initial new http request by using HttpClient and HttpPOST, here is example code :
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(<"your url for each request">);
httpPost.setHeader("<header-name>", "<header-value>");
HttpReponse httpResponse = client.execute(httpPost);
//here omit getting content-type and encoding
InputStream reponseInputStream = httpReponse.getEntity().getContent();
Then you can put responseInputStream to return WebResourceResponse(<content-type>, <encoding>, reponseInputStream) in your shouldInterceptRequest(Webview,url)
if you have any request which doesn't need add more header, just filter it and return null, shouldInterceptRequest(Webview,url) will do the rest.
Hope this can help.

how to sync cookies between android.webkit.CookieManager and java.net.HttpUrlConnection on Android

I'm making a progam on Android.
It has one button and one WebView. If I click the button, It send post message to a web server. (using httpurlconnection)
The webview load a page from the same server. I want to sync cookies between core and webview. I found the method from core to webview. But, I couln't find a method from webview to core.
I tried these order.
the page of webview is loaded.
read cookies from webview.
add these cookies to core cookies using HttpCookie.
URI uri = new URI(url);
HttpCookie httpCookie = new HttpCookie(cookie.getKey(), cookie.getValue());
((java.net.CookieManager) CookieHandler.getDefault()).getCookieStore().add(uri, httpCookie);
but It doesn't working. what is wrong? please advice me about it.
Thank you in advance.
you need to set the cookie to your HttpPost object as well, try this:
httpPost.setHeader("Cookie", "PHPSESSID=" + httpCookie.getValue());
where httpPost is an instance of org.apache.http.client.methods.HttpPost

HttpPost: No "Set-Cookie" Header

I want to get the session cookie of a website. Unfortunately the "Set-Cookie"-Header doesn't show up.
Here's the code I've written:
"commands" is a String[][] and the whole code is wrapped by try/catch.
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE,cookieStore);
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>(0);
for (int i=0;i<commands.length;++i)
nvps.add(new BasicNameValuePair(commands[i][0],commands[i][1]));
httppost.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
Header[] headers = response.getAllHeaders();
List<Cookie> cookies = cookieStore.getCookies();
String data = EntityUtils.toString(entity);
My understanding of Http Communication tells me that there should be a "Set-Cookie" Header. The only Headers I get from response.getAllHeaders() are Connection:close, X-Powered-By:PHP/4.3.4 and Content-Type:text/html
There is a bit of javascript included in the returned data (response.getEntity()).
<script language = "javascript">
<!--
location.href="/index.php";
function SetCookie(name,value,expire,path){
document.cookie = name + "=" + escape(value) + ((path == null) ? "":(";path="+path))
}
var iad = 461180104
SetCookie("iad",iad,0,"/")
-->
</script>
As far as I understand this, this code is never executed because it's just a comment ?!
But as well this is probably the bit where the cookie should be created.
Any ideas?
UPDATE:
"Opera Mobile" is the only browser for Android I found which has no problem with cookies on this site. "Opera Mini", "Dolphin HD" and the Froyo Stock browser all fail. No Desktop browser has problems connecting. Is this a webkit issue? And if this is the case: how to avoid it?
Using Chrome's developer tools or Firebug, check the HTTP response for the "expires" parameter in the Set-Cookie header field. Make sure the time / date settings on the phone are set correctly. If the browser thinks the cookie is already expired, it won't store it.
If that doesn't work try using wireshark / tshark to grab a trace of the communication from your client, and compare it to a browser that's working the way you expect it to.
By the way, the comment delimiters around that bit of Javascript don't prevent the script from being run; they just prevent older (really old) browsers from trying to render the script in the document. That cookie ("iab") doesn't look like the cookie for authentication. There's likely an http-only cookie with a session identifier; you should be able to see it using the aforementioned Firebug / Dev tools.

android hidden webview, Is it possible?

I want to build an application which takes user/pass information from user and use it on an https webpage and extract the returned raw html code from that page.
I want to know is this possible?
If so, what sort of class i should use. Can it be done with some hidden webview mechanism. Sine i think we can access the java script variable in a webview from our application. So is this possible. Or i am just wasting my time in this direction.
If you are asking if it is possible to 'download' a webpage without actually displaying it in a WebView then try this...
HttpClient client=new DefaultHttpClient();
HttpGet getMethod = new HttpGet(Url);
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String response = client.execute(getMethod, responseHandler);
EDIT: Sorry - the Url parameter passed to HttpGet() above is actually a String variable containing a url to the wep page you want.
Pretty old subject here, but I see that I'm not the only one facing this situation.
This is how I understand it:
HttpClient objects and WebView objects will not share the same "session" scope through your application.
That is, if you have this URL that performs authentication and starts a valid session on a website; and following that, you try to open one of those website pages - session protected - through the webview, the webview will act as if no session were available. WebView is not aware of the HttpClient session.
A solution to that is to call the URL that starts a session through the WebView.postUrl method like this:
webView.postUrl(url, EncodingUtils.getBytes(postParameters, "BASE64"));
I'm sure this can be done in a hidden webView.

Android: Example for using a cookie from HttpPost for HttpGet

I am able to use the example here: http://www.androidsnippets.org/snippets/36/index.html and successfully get the "HTTP/1.1 OK" response for a webesite I am sending the HttpPost along with the user credentials. However, I am unable to use an HttpGet to further browse other pages on this site.
Can anyone please let me know, what's going wrong. I am sorry - I am very new to Java.
My guess would be that when the website gets the Post and logs the user in, it sets cookies on the response to indicate that the user is logged in, and then requires those cookies on subsequent Get's.
You will need to do something like the following (this is borrowed from a bigger app so may not compile right out of the box)
DefaultHttpClient mHttpClient = new DefaultHttpClient();
BasicHttpContext mHttpContext = new BasicHttpContext();
CookieStore mCookieStore = new BasicCookieStore();
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);
This sets up a cookie store within the HTTP context, and you then use that context on Get's and Post's. For example...
HttpResponse response = mHttpClient.execute(mRequest, mHttpContext);
Under the covers the HTTP client will store cookies from responses, and add them to requests.

Categories

Resources