how to post a form with some cookie - android

i have posted a question for htmlunit in this link: how to use htmlunit with my android project
mainly i have a link, which i have get after login (i have login through web view) this link give me a simple page. in that page there is a textarea and a submit button. and there are some javascript too (i think these javascript run, when i press the submit button). i can do it through webview, but for some reason i don't want to use webview. whene i press submit button, it deliver the value of textarea and some value of hidden field with existing cookies(which are get when i logged in through webview) Post method. i need to do this without webview. now is there any other option beside htmlunit ?? i heard about HttpClient, HttpUrlConnection. but i don't know how to use them to solve my problem, because they are totaly new to me. i think if i use these class i have to run them in a seperate thread from UI tread. one more thing, after submitting it will redirect me to another page. i don't need to do anything with this redirected page.
thank you

this is the same answer which i have given here
i have solve the problem. first of all i was getting the right cookie all time. so what was the problem then. either i was wrong to integrate the cookie with Jsoup or Jsoup was doing something wrong. so, first i have get the page with HttpUrlConnection and then parse it with Jsoup. like this:
URL form = new URL(uri.toString());
HttpUrlConnection connection1 = (HttpURLConnection)form.openConnection();
connection1.setRequestProperty("Cookie", my_cookie);
connection1.setReadTimeout(10000);
StringBuilder whole = new StringBuilder();
BufferedReader in = new BufferedReader(
new InputStreamReader(new BufferedInputStream(connection1.getInputStream())));
String inputLine;
while ((inputLine = in.readLine()) != null)
whole.append(inputLine);
in.close();
Document doc = Jsoup.parse(whole.toString());
any advice about this code would be appreciated.

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!

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 :)

Android Service that searches in a website

This is my very first thread so please bear with me. :)
I want to create an Android Service that searches a specific string on a website. To do this I have tried to download that site and search within the html code but the application always crashes when trying to download it.
Does anybody have any idea how to do this?
Thanks in advance
I had a similer problem when i started making an android app that scans imdb.com for movie information. After a lot of searching the internet and testing things out i came up with this:
import java.net.*;
import java.io.*;
URL url = new URL("websiteToLookAt");
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader bufferedReader = new BufferedReader(isr);
String lineThatIsBeingRead = null;
String theString;
while((lineThatIsBeingRead = bufferedReader.readLine()) != null){
if(lineThatIsBeingRead.contains("StringYouAreLookingFor")){
theString = lineThatIsBeingRead;
break;
}
}
The first line sets up the URL of the website you are scanning
The second line opens a the internet to allow you to access the html source directly
The third line makes a Buffered reader that is able to read the source the InputStreamReader gives it
The fifth line is the string that the current line of HTML source is being held in while the buffered reader is checking if it contains the right string. (string1.contains(string2) looks at wether or not string2 is in string1. example: String myName = "john"; if you were to test if myName.contains("oh"); it would return true)
The Sixth line is the string that you will put the string you are looking for from the HTML source(like if you were looking for the name of a movie, this would be the string you would assign the name to)
The while loop reads the next line of the html source code every time the loop starts over and sets the line it just read to the String variable lineThatIsBeingRead. it will keep doing this as long as there is a new line to read. When the buffered reader comes to the end of the HTML source the conditions for the while loop return false and it breaks the loop.
The if statment checks to see if lineThatIsBeingRead has the string StringYouAreLookingFor in it. if it does, it sets theString(the string you are looking for) to lineThatIsBeingRead(the string that is in the buffered reader) then it breaks the while loop. otherwise, it resets the while loop and it starts all over again.
I have the variable theString in there because i was looking for several strings, but if you only need to find one string you can delete lines 6 & 11 and just have lineThatIsBeingRead as the string you assign the string you are looking for to.
Another thing to keep in mind is java doesn't allow you to connect to the internet through the UI thread(the way java wants you to do it is with an intent so when you publish the app remember to take this out and make it run on an intent). but if you add
if(BuildVERSION.SDK_INT >= 9){
StrickMode.ThreadPolicy Policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(Policy);
}
to the onCreate() method and it will bypass that rule.
Hope this was helpful, and good luck with your project!

how to get all cookies or cookie's url from android.webkit.CookieManager

mainly, i have loged into facebook using webview. so, i don't know which cookies for which urls are saved into CookieManager. i don't know whether it is possible or not, but i have know idea how to do it.
now, i need to get a page using Jsoup. but i need to pass some cookie also to get the page, otherwise server will return me an error pager.
i want it with Jsoup, because i need some information from the page
i have trying something like this, but all the time i am getting the error page:
Map<String, String> cookies = new HashMap<String, String>();
cookies.put(domain1, my_cookie1);
cookies.put(domain2, my_cookie2);
cookies.put(domain3, my_cookie3);
cookies.put(domain4, my_cookie4);
Document doc = Jsoup.connect(uri.toString())
.cookies(cookies)
.timeout(10000)
.get();
Log.e("title", doc.title());
my_cookie's are get from CookieManager. and they are not null, because i have printed them.
i think the problem is with cookies. or not? is there any solution. i think i am missing some cookie from CookieManager.
i need to get the page.
Edited:
or, is it possible to pass the cookimanager to Jsoup ? so, that it can take the cookie from cookiemanager directly. or, can i know which cookies are needed for getting my desire page?
i have solve the problem. first of all i was getting the right cookie all time. so what was the problem then. either i was wrong to integrate the cookie with Jsoup or Jsoup was doing something wrong. so, first i have get the page with HttpUrlConnection and then parse it with Jsoup. like this:
URL form = new URL(uri.toString());
HttpUrlConnection connection1 = (HttpURLConnection)form.openConnection();
connection1.setRequestProperty("Cookie", my_cookie);
connection1.setReadTimeout(10000);
StringBuilder whole = new StringBuilder();
BufferedReader in = new BufferedReader(
new InputStreamReader(new BufferedInputStream(connection1.getInputStream())));
String inputLine;
while ((inputLine = in.readLine()) != null)
whole.append(inputLine);
in.close();
Document doc = Jsoup.parse(whole.toString());
any advice about this code would be appreciated.

Android: is it possibile to alter the WebView class in order to have access to the html of the loaded page?

I need to use a WebView to load certain webpages and dynamically change the css before showing them to the user (which means I have to delete all <link> tags and append the one with my css). (Why? Because I want to adapt the look of a particular site - which is not mine - for smartphones)
Now, I've seen that similar questions have been answered that the only way to modify the html before showing it to the user is by executing some javascript in the onPageFinished method; this could be a solution, but I'd like to consider other possibilities as well.
So, my questions are:
1) If I go deeper in the source of the WebView class, is it possible to find where the html is loaded from the site, so that I have direct access to the html and I can modify it as I want?
2) If yes, is WebView the class that handles the communication and retrieves the html? If else, which one is it?
3) Assuming that what I asked is possible, do you think that the application would perform better if the modification to the html where made this way instead of using javascript?
You can use HttpClient to perform an HTTP GET and retrieve the HTML response, something like this:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
You can now have fun with your String html and place it in the Webview
WebView webview=(WebView)findViewById(R.id.mywebview);
webview.loadData(myModifiedHtml, "text/html", "UTF-8");
You can do it easily by enabling JavaScript on your webview and executing
document.getElementsByTagName('html')[0].innerHTML
Check this answer for detailed procedure.

Categories

Resources