How to stay logged in using a HttpClient on Android? - android

I have a problem with Androids HttpClient / HttpRequest that's really giving me headache.
The program should login on a website (it's actually working and login is successful). After it's logged in, I want it to get another path of the site, but I don't know how to stay logged in for the next call. The site is using cookies and i tried four different examples that i found on stackoverflow, but nothing helped me.
The code I have is:
HttpClient loginclient = new DefaultHttpClient();
HttpPost loginpost = new HttpPost("EXAMPLEURL");
//Parameters
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("cookieuser", "1"));
nameValuePair.add(new BasicNameValuePair("vb_login_username", "EXAMPLE"));
nameValuePair.add(new BasicNameValuePair("vb_login_password", "EXAMPLE"));
nameValuePair.add(new BasicNameValuePair("do", "login"));
nameValuePair.add(new BasicNameValuePair("submit", "anmelden"));
// Url Encoding the POST parameters
try {
loginpost.setEntity(new UrlEncodedFormEntity(nameValuePair));
}
// Making HTTP Request
try {
HttpResponse response = loginclient.execute(loginpost);
String responseString = new BasicResponseHandler().handleResponse(response);
System.out.println(responseString);
}
PS: I removed the catch statements to make the code more clear

Related

Http GET Request Android

I know that it's possible to put some data in GET request body, but I know that it's not a good practice.
Though I want to do it but i don't know how. I searched the web but couldn't find anything.
Can you give me some little code examples or reference me to some usefull info.
Let's say we have this code, how to add nameValuePairs to request body of GET request :
String url = "something";
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpGet httpGet = new HttpGet(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("dtype", "0"));
nameValuePairs.add(new BasicNameValuePair("category_id", ""));
HttpResponse response = httpClient.execute(httpGet);
System.out.println("log in");
Thanks in advance.

Login and download programmatically in Android

I want to make an application for a web page that needs POST login. After that I'll download an HTML page and read its tags and perform some tasks to display some information.
Before I begin, I want to know if there is a way login in to this page using POST, somehow programmatically and download the page.
this is a way to send post request:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com/login.php");
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", "value"));
Httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String result = EntityUtils.toString(response.getEntity());
}
catch(Exception e)
{
e.printStackTrace();
}
you can send your information in order to login to page.!

Making an HTTP post request to the Google Play Android Developer API

I'm trying to authorize the Google Play Android Developer API. I'm at the step where I need to make an HTTP post request to exchange the authorization code for an access token and a refresh token. Google gives the following example request:
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code
I'm confused... First of all, for an installed application (Android) no client_secret is given. I created a web application for the same project in the Google API Console and this gave me a client_secret, so I used that, even though there is no web application. The following code gives me an "invalid_grant" error:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://accounts.google.com/o/oauth2/token");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("code", "CODE"));
nameValuePairs.add(new BasicNameValuePair("client_id", "CLIENT_ID"));
nameValuePairs.add(new BasicNameValuePair("client_secret", "CLIENT_SECRET"));
nameValuePairs.add(new BasicNameValuePair("redirect_uri", "urn:ietf:wg:oauth:2.0:oob"));
nameValuePairs.add(new BasicNameValuePair("grant_type", "authorization_code"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
....
Taking out the client_secret entirely gave me an "invalid_request" error.
This is how I solved it. I ended up using a Web Applcation. See more details in my response here.
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token"));
nameValuePairs.add(new BasicNameValuePair("client_id", CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
nameValuePairs.add(new BasicNameValuePair("refresh_token", REFRESH_TOKEN));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
I have managed to redeem access code for access token from android app without the help of web application by simply eliminating the client_secret key as it is not applicable for installed applications.
HttpPost httppost = new HttpPost("https://accounts.google.com/o/oauth2/token");
httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("grant_type", "authorization_code"));
nameValuePairs.add(new BasicNameValuePair("client_id", BLOGGER_CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("redirect_uri", "http://localhost"));
nameValuePairs.add(new BasicNameValuePair("code", BLOGGER_ACCESS_CODE));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient httpClient = new DefaultHttpClient(myParams);
response = httpClient.execute(httppost);
String returnedJsonStr = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = new JSONObject(returnedJsonStr);
String receivedToken = jsonObject.getString("access_token");
Reason to post this comment is that your solution can be misleading to someone who might think only way to get access token in mobile apps is via web-application, which I thought after reading your post few minutes ago !
To avoid invalid_grant error follow this code:
https://stackoverflow.com/a/14141020/989418

Extract HTML from HTTPResponse

I am having difficulty finding a solution to this problem, so I am hoping you can help me. I am logging into a website using HTTPost. I can successfully log in, but still there remains a problem. When logging in, it first takes me to a "Middle man page" that redirects me to the main home page which seems to be using Window.onLoad function to redirect me. When I get the HTML of the response, it is getting the HTML of the redirect page. The website uses a session to keep me logged in, not a cookie. I just need to get the HTML of the FINAL page I am redirected to. Here is my code. Thanks.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"https://secure.groupfusion.net/processlogin.php");
String HTML = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs
.add(new BasicNameValuePair(
"referral_page",
"/modules/gradebook/ui/gradebook.phtml?type=student_view&sessionid=16beb2d321c8c845b751022a196dbeb1"));
nameValuePairs.add(new BasicNameValuePair("currDomain",
"beardenhs.knoxschools.org"));
nameValuePairs.add(new BasicNameValuePair("username", username
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password", password
.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
String g = httppost.getURI().toString();
HttpResponse response = httpclient.execute(httppost);
HTML = EntityUtils.toString(response.getEntity());
sting.setText(HTML);

HttpPost returns only part of response body

I'm performing login on server using HttpPost method.
CommonsConnection.initHttpConnection();
HttpResponse response;
String contentOfMyInputStream;
HttpGet initPostMethod = new HttpGet("https://stat.volia.com:8443/ktvinet/index.jsp");
response = CommonsConnection.hc.execute(initPostMethod);
HttpPost Statistics = new HttpPost("https://stat.volia.com:8443/ktvinet/j_security_check");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("j_username", "username"));
nameValuePairs.add(new BasicNameValuePair("j_password", "password"));
nameValuePairs.add(new BasicNameValuePair("submit", "%C2%EE%E9%F2%E8"));
Statistics.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = CommonsConnection.hc.execute(Statistics);
HttpGet mainPage = new HttpGet("https://stat.volia.com:8443/ktvinet/main.do");
response = CommonsConnection.hc.execute(mainPage);
contentOfMyInputStream = EntityUtils.toString(response.getEntity());
The response I get - body of webpage - is like 1/5 part of webpage, which ends with "...".
Am I missing some parameter for httpclient ?
How are you examining the response? if you're looking int he debugger or something similar, it may well truncate long strings, try dumping it in a file.
Logcat displays only part of long string response.

Categories

Resources