Android: how to connect to a webpage programmatically using HttpsURLConnection

I'm new to Android (and Java too), so sorry if my problem is the main suggestion! I have to write an Android application, whitch icons on an aspx web page in the background, get some data from it and then exit the form on the web page. (and do it programmatically)

Basically, the procedure is like getting a list of email addresses from Gmail:
1. go to the page "https://mail.google.com" and follow these steps: 2. click "Contacts" (== go to the page "https://mail.google.com/mail/?shva=1&zx = dzi4xmuko5nz # contacts ")
3. Extract the page using HttpsURLConnection (or something like this) and receive emails in the object (for example, Map or String)
4. Click on the "Logout" link.

I hope this is clear. Looking on the Internet, I find a solution only for "sampling", so this is not a problem. But I have no idea about the "click of a piece."

...... // Get the connection URL myurl = new URL("https://mail.google.com"); HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection(); // complete the fields con.setRequestProperty("Email","myacc"); con.setRequestProperty("Passwd","mypass"); /* * in this part, should make sign in, and go directly to contacts... * I don't have any idea how to do it... */ // for the present, just write out the data InputStream ins = con.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(ins)); String inputLine; while ((inputLine = in.readLine()) != null) { Log.d("Page:"," "+inputLine); } in.close(); /* * And here should be the "Sign out" part */ ...... 

Any help would be great, thanks for that! (and sorry if my english is not so good ...)

EDIT: The problem is resolved. Thanks!

  ....... String GMAIL_CONTACTS = "https://mail.google.com/mail/?shva=1#contacts"; String GMAIL_LOGIN = "https://mail.google.com"; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(GMAIL_LOGIN); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); nameValuePairs.add(new BasicNameValuePair("Email", MY_ACC)); nameValuePairs.add(new BasicNameValuePair("Passwd", MY_PASS)); nameValuePairs.add(new BasicNameValuePair("signIn", "Sign In")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpClient.execute(httpPost); Log.d(TAG, "response stat code " + response.getStatusLine().getStatusCode()); if (response.getStatusLine().getStatusCode() < 400) { String cookie = response.getFirstHeader("Set-Cookie") .getValue(); Log.d(TAG, "cookie: " + cookie); // get the contacts page HttpGet getContacts = new HttpGet(GMAIL_CONTACTS); getContacts.setHeader("Cookie", cookie); response = httpClient.execute(getContacts); InputStream ins = response.getEntity().getContent(); BufferedReader in = new BufferedReader(new InputStreamReader( ins)); String inputLine; while ((inputLine = in.readLine()) != null) { Log.d(TAG, " " + inputLine); } in.close(); } else { Log.d(TAG, "Response error: " + response.getStatusLine().getStatusCode()); } ....... 
+4
source share
3 answers

A “click” basically sends a request to the server and displays the return information.

1 / find out what url to call this request (if it is a web page, see for example firebug)

2 / find out what parameters, find out if the method is GET or POST

3 / is played back programmatically.

4 / a "login phase" probably implies the use of a cookie that the server gives you, and that you must send back after each request

However, your approach is wrong. You should not try to connect directly to Google via URL connections. (Also you should use HttpClient). Moreover, query properties are not parameters. They are the headlines.

I highly recommend you start with something simpler to conveniently communicate with HTTP in java, GET, POST, parameters, headers, answers, cookies ...

Edit

After receiving the response, you will want to verify that

 response.getStatusLine().getStatusCode() < 400 

He will tell you that the login was successful. (2xx are successful, 3xx are moving, etc. 4xx are errors in the request, 5xx are errors on the server side, Gmail responds with 302 for login in order to suggest redirection to incoming messages). Then you will notice that the “Set-Cookie” response has a specific header containing the cookie that you want for future connections in order to:

 String cookie = response.getFistHeader("Set-Cookie"); 

Then you can call a request to get contacts:

 HttpGet getContacts = new HttpGet(GMAIL_CONTACTS); getContacts.setHeader("Cookie", cookie); response = httpClient.execute(getContacts); InputStream ins = response.getEntity().getContent(); 

It should be something like this.

+5
source

What you are trying to do is parse the Gmail's httml page. This is the wrong approach, as Gmail uses javascript to create the page. Your code will have to emulate a browser (execute javascript) for this to work.

If you only need read access to Gmail, use the Gmail Inbox API . This gives you access to unread messages in your inbox.

If you need full access, see Gmail’s IMAP access . Since IMAP is a different protocol, then you will need to use a separate IMAP library for java. See this tutorial .

+1
source

You should consider using a mail request to send data to the server: Sending POST data to Android

Using connection properties has nothing to do with what you want to achieve.

Regards, Stéphane

0
source

All Articles