Android $_POST is empty at server? - android

I am writing an httppost on android but on the server side, the $_Post is empty, I don't understand what I am doing wrong. Here is the code I used.
HttpPost post = new HttpPost(url);
List<NameValuePair> pairs = new ArrayList<>();
pairs.add(new BasicNameValuePair(type, login));
pairs.add(new BasicNameValuePair("password", password));
pairs.add(new BasicNameValuePair("tag", tag));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = new DefaultHttpClient().execute(post);
The php code
<?php
print_r(array_values($_POST));

check your url.
don't forget http:
"http://yoursite.com"
check response code to be 200:
response.getStatusLine().getStatusCode() ==200

Related

HttpPost encoding to windows-1255 add 25 to each char

I tried to send data to server with httpPost.
its look like this:
String username = URLEncoder.encode("myUsername","windows-1255");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("****");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username", username));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
But it's sending to the server char as %25XX and not %XX.
Why it's happening?
I was need to add the entity as string and not as list:
String param="username"+username;
post.setEntity(new StringEntity(param));

Android cant execute POST request after GET

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

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.

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

Post data to server in android

I want to send some data to a server through POST method in android. I am using the following code
DefaultHttpClient hc=new DefaultHttpClient();
ResponseHandler <String> res=new BasicResponseHandler();
HttpPost postMethod=new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name", "value"));
nameValuePairs.add(new BasicNameValuePair("password", "value"));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
String response=hc.execute(postMethod,res);
But I am getting the error response in my response xml. The error message is cookies are disabled in client machine. How do I need to enable cookies in android ?
You need to handle cookies with your request. See this and this related questions.

Categories

Resources