android hidden webview, Is it possible? - android

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.

Related

Android Webview POST request

I'm trying to do a post request with a WebView on Android.
After searching for days and trying dozens of things i couldn't get it work. In SWIFT it's just a few lines of code so i thought there must also be a simple way to do a post request for a webview on android.
As (for 2016) EncodingUtils and HTTPClient are deprecated this are my current approaches:
String url = "http://example.com/php.php";
String postData = null;
postData = "param1=" + URLEncoder.encode("1234567890", "UTF-8");
webcontent.postUrl(url,postData.getBytes());
//or
webcontent.postUrl(url, Base64.encode(postData.getBytes(), Base64.DEFAULT));
Both just result in a blank screen. There is just one parameter to be sent and a string containing html from the server should be received.
In addition, the php on the server returns a html-string with colored background irrespective of any input, but even this isn't displayed so maybe the whole request never reaches the server?
Thanks in advance!
In Android you do not use webView to access the content of the HTTP response. You'll need to use HttpClient for that purpose!
See this nice tutorial which explains the fundamentals! Also see this video if you find it hard!
Hope it helps!

How do I call Web Service written in Python from Android app

Here is my web service code
I can call it in my browser using http://sheltered-taiga-3258.herokuapp.com/toi/<input parameters> I am collecting Input parameters from user on android device. Obviously web service returns a JSON data which I need to display at client side in android application. I went through many posts and tutorials on android and web service but was not successful as many have the web service example of POST request and service in PHP. I want to do it for GET and service is in flask.
Please help Thank you.
EDIT:
I am calling web service using HttpGet object and I am passing my URL as parameter to it.
HttpGet httpget = new HttpGet(myURL);
and I am Constructing myURL as
EditText inputString = (EditText) findViewById(R.id.serverText);
String appendString =URLEncoder.encode(inputString.getText().toString(), "UTF-8") ;
private final HttpClient Client = new DefaultHttpClient();
String myURL = "http://sheltered-taiga-3258.herokuapp.com/toi/" + appendString;
Here I am getting myURL as
http://sheltered-taiga-3258.herokuapp.com/toi/hc+stays+toll+collection but I want it in this manner
http://sheltered-taiga-3258.herokuapp.com/toi/HC%20stays%20toll%20collection%20in%20kolhapur%20city
I know there is some url encoding problem but dont know way out of it.
Here is what gave me solution to my question may not be the suggestible and standard way of doing it but got me out of the problem:
String appendString = (inputString.getText().toString()).replace(" ", "%20") ;//here %20 is encoding for space
String myURL = "http://sheltered-taiga-3258.herokuapp.com/toi/" + appendString;
That gave me this :
http://sheltered-taiga-3258.herokuapp.com/toi/HC%20stays%20toll%20collection%20in%20kolhapur%20city
instead of this
http://sheltered-taiga-3258.herokuapp.com/toi/hc+stays+toll+collection
If I find the authentic way of doing this thing I will surely be posting it here marked as answer

sync cookies between webview and java

I am almost dead doing this.Need help.
My Requirement :-
I am developing an android app and want to use the cookies(/session) from webview in my java code. I basically want to get the html of other pages of an url after login in webview without opening those pages in webview but through my java code.
What I tried :-
For this I tried HttpClient and HttpURLConnection referring many SO questions but failed.
Can anybody please give me a sample code?
Say I have cookies in a hashmap cookies. How Can I use HttpClient and HttpURLConnection or anything else to get the other page html. The website I am trying with is https:
Please give a sample code
If I am right u are trying to use your webview's cookies to get other pages of site in your activity java code.if yes try this:
BufferedReader reader = null;
try {
URL url2 = new URL("url");
URLConnection con = (URLConnection) url2.openConnection();
CookieManager.getInstance().setAcceptCookie(true);
con.setRequestProperty("Cookie",CookieManager.getInstance().getCookie("logged in url in webview"));
con.setDoOutput(true);
con.connect();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBiffer html;
String line = "";
while ((line = reader.readLine()) != null) {
html.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
It worked for me.
Have you tried to add the cookie as a HTTP header? I am not sure if I have understood you right but you can consider these:
Cookies: If you want to load some resource (no matter if it is a web page, image, css, js or something else) you are making a HTTP request. If the server keeps a session for your user, you are probably given a session cookie. The cookie must be sent with each request to the server as e COOKIE header. So if you want to pass the cookie to your request, add it as a header. Android provides you an easy way to do this with the CookieManager class. You can refer to this.
SSL: If you are trying to access a secured web site (https) you have to use an SSL certificate. Android comes up with a bunch of predistributed certificates for most of the popular web cites (e.g Facebook, Google, Twitter, etc.). You can use them out of the box. If your SSL certificate is not presented, you have to add it manually. Read this for more information.
I hope this was useful :)

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

Authentication header issue in webview

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.

Categories

Resources