Android cant execute POST request after GET - android

My problem is this: after the GET request authorization and save cookies trying to perform a POST request to add data, but the server responds with 500 code. What's funny, because if POST query string form in a browser, it is executed correctly. The code below.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(site + "/admin/users/login_do/?login=admin&password=demo");
HttpResponse response = httpclient.execute(httpget);
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
HttpPost httppost = new HttpPost(site + "/admin/news/add/5/item/do/");
httppost.addHeader("Cookie", "PHPSESSID="+cookies.get(0).getValue()+"; umicms_session="+cookies.get(1).getValue()+"; stat_id="+cookies.get(2).getValue());
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("active", "1"));
nameValuePairs.add(new BasicNameValuePair("name", "test"));
nameValuePairs.add(new BasicNameValuePair("data[new][anons]", "anno"));
nameValuePairs.add(new BasicNameValuePair("data[new][content]", "cont"));
nameValuePairs.add(new BasicNameValuePair("data[new][publish_time]", "1420202020"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
response = httpclient.execute(httppost);
StatusLine status = response.getStatusLine();
Log.d("my",String.valueOf(status.getStatusCode()));
Log.d("my",String.valueOf(status.getReasonPhrase()));
I've tried to run POST using HttpURLConnection, but also received 500 response.
Who can tell what might be the problem?

Problem solved. I Forgot to send csrf token with POST request and set Referer header
nameValuePairs.add(new BasicNameValuePair("csrf", csrf));
httppost.addHeader("Referer", referlink);

Related

Crash on Sending HTTP post request from android

I was trying to send HTTP request from Android with using the pieces of codes I found in SO but I ve got two problems. First one is how can I put a header to my http request. Second one is when I try to execute it(httpclient.execute) I get the following error. I already tried the url with advanced rest client and got a valid response.
Here is a successfull request
POST /api/v1/login/ HTTP/1.1
HOST: api.example.com
user-agent: Example
content-type: application/json
content-length: 47
{"username":"test", "password":"123456"}
Here is my code.
String restUrl="https://api.example.com/api/v1/login/";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(restUrl);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", mJidView.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password",mPasswordView.getText().toString() ));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
I get error in httpclient.execute(httppost)
Here is the additional logcat.
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
http is not recomemended to send requests you can change it to volley.
see this link:
volley Google developers
and if you need help you can try search here or in the web.
Probably you got StrictMode enabled and you make your http request in main thread which is not permitted in Android right now.
Please make your request in working thread, or change StrictMode policy:
Error StrictMode$AndroidBlockGuardPolicy.onNetwork
But generally - you should make request in working thread.
Have you tried setting a header for your Post request? like this
httpPost.addHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Accept-Charset", "utf-8");
httpPost.setEntity(new UrlEncodedFormEntity(NameValuePairs, "UTF-8"));
Edit:
String restUrl="https://api.example.com/api/v1/login/";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(restUrl);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", mJidView.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password",mPasswordView.getText().toString() ));
httpPost.addHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Accept-Charset", "utf-8");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

Can't Log to an aspx form from Android

I have a code to make an httpPost, but i can't log using the parameters in the array, but if i make all the complete url i will log with no problem !, what am i missing ?
String URL = "http://domain.com/projexct/form.aspx"; //won't work
// String URL = "http://domain.com/projexct/form.aspx?user=aa&pass=11";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", "aa"));
nameValuePairs.add(new BasicNameValuePair("pass", "11"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
EDIT1
It doesn't throw and error, but if i use the parames in namevaluePairs it shows just the login page, if i use the URL commented it will actualy log in.

Get the authentication from the moodle for a android application

I want to get the authentication from moodle (say the moodle site from our university) in my android application. And I do not have the access to the database. I tried to make a http post request from my application with username and password.
But when I execute the post request, I get an exception. This is what I have tried
private URI url = new URI("https://online.mrt.ac.lk/login/index.php");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "user"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
I get a exception like this
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
Please someone help me.

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

Why is HttpPost using a GET method? [Android]

I am executing the following post in Android:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("account", "login"));
nameValuePairs.add(new BasicNameValuePair("email", "email#email.com));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
In the server side I return the cgi.request_method variable and it is GET.
Shouldn't it be POST? Am I missing something?
That code should initiate a POST, not a GET. I would guess that something on the server side is misconfigured.
My problem was send http instead of https

Categories

Resources