Can't finally perform HTTP POST request - android

I work on my first Android application and I have some great problems with trying to make HTTP POST request and receive response. These are few facts:
I have INTERNET permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
I tried many solutions. Solutions with URLConnection or HttpURLConnection don't work because they return empty string. Solution with HttpClient and HttpPost fails on execute() - I was googling for an hour but didn't find out how to fix that.
There is internet connection. But I don't know how to fix my problem and finally to send HTTP POST request.
upd: e.g. this code makes my program crash:
public static APIResponse getResponse(String action, Map<String, String> params) throws IOException {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://google.com");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}
upd 2: it works on my phone but doesn't work on emulator! how to fix that behaviour on emulator?
upd 3: fixed.

Look at the example for HTTP Post
You should also employ fiddler2 to help debug your HTTP messages.
Also please note that you are not catching all exceptions properly ... you can add a generic catch statement at the end to prevent your app from crashing and help you figure out where the problem lies. Could be a timeout or similar.

Related

Is there any way to create a mini-proxy for http on android?

I am working on a HttpClient that posts something to a website. The client looks something like this (based on this link):
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
What I want to do now is to create a small proxy service (a VERY simple one) that runs on the same android device and listens for the outgoing HTTP connexions and modifies the POST ( for example change the "id": "12345" into "54321", just for testing purposes), and then pass the HttpRequest to the actual website. How cand I do something like that? I've been googling around but found nothing that could give me an idea how to do it(like a tutorial or something). Can anybody give me an idea of how to do that?
You do not need to write code to achieve this. Instead of writing your own proxy and since you only need it to test you application, you can use the mitmproxy which is a man-in-the-middle proxy (based on your description that is what you want to achieve). Follow this tutorial to set up your PC as the proxy and the device as your client.
http://blog.philippheckel.com/2013/07/01/how-to-use-mitmproxy-to-read-and-modify-https-traffic-of-your-phone/

Android sends GET instead of POST

I have a backend for my android app, which returns 404 on GET and json on POST. Now, I'm trying to do POST request using this snippet:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/api/login");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", "email#email.com"));
nameValuePairs.add(new BasicNameValuePair("password", "qwerty"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Server however receives GET request. With curl POST backend returns json as expected. But somehow httpPost sends GET(!) request. What could be the problem? What am I doing wrong?
Ok guys, answered my own question pretty quickly, may be helpful for others.
I replaced hostname with ip address, and it worked!

Android : Post data to a website via app and view results in app

I have an application which converts a website to mobile format. There is a spinner in the app. So my doubt is, how do i pass these selected values from my spinner to the website to get results?
You need to make a http post request.
http://developer.android.com/reference/org/apache/http/client/HttpClient.html.
NOTE : If you are making network related operation you should use a AsyncTask other wise you will get a NetworkOnMainThreadException (Honeycomb and later).
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Some links with source code available in the links below
http://www.androidhive.info/2011/10/android-making-http-requests/
http://android-er.blogspot.in/2011/09/example-of-httppost-on-android.html
Since you are running the a mobile web application inside a WebView container you can use a well known library like jQuery an its ajax stuff.

Android Pen Testing

I have been pen testing a random android app that uses POST method to send data to a remote server using HTTPS.
I have set up a proxy and am able to intercept the traffic, however the POST method appears to be encrypted and "url-encoded" .
What i want to know is .. is there a common encryption standard followed in such a scenario something like the Base64 or would it be so that the application uses a signature encryption mechanism internally which encrypts the data before it is sent through the POST method.
Any guidance would be really appreciated. Thanks in advance !
You can set encoding to whatever you like
UrlEncodedFormEntity(nameAndValuePairsGoHere,"encoding goes here");
Full example, credit: Source
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}

HttpServletRequest get no parameters from my httppost

I have just a curiosity question. I have an HttpPost request in Android that looks something like this:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(getString(R.string.url));
//This code does not work
HttpParams params = new BasicHttpParams();
params.setParameter("type", "20");
post.setParams(params);
try {
HttpResponse response = client.execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
On my server side, I have a servlet that listens for requests and parses the parameters:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()){
System.out.println(en.nextElement());
}
}
When I execute this code, the servlet does not see any parameters at all. But if I replace the whole "parameter" chunk with this code:
//This code works
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("type", "20"));
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
My servlet can parse parameters. It's not a problem, I'm just going to use the entity but my question is, why can't my servlet getthe parameters from the first code chunk? What's wrong with setParams? Why can the servlet see parameters if I make them an entity?
In HTML when we have something like "http://host/path?user=uname&passwd=pass", we call the part (user=uname&passwd=pass) after the question mark "form data".The "form data" can be attached to the end of the URL after a question mark (as above), for GET requests, or sent to the server on a separate line, for POST requests.The "form data" are split to parameters. The parameters are separated by & when we use GET.
In our case the HttpPost and HttpGet classes extend the AbstractHttpMessage which implements the setParams method. This method is same for GET and POST but does the job only for GET! In the case of GET the parameters are put in the URL. In the case of POST you need to set the entity for the parameters to be on a "separate line".
On the server side when using servlets the getParameters is clever enough to find the parameters for GET and POST.
Thats why on the server side we do not need to change the code for getting the parameters!
Hope I helped!

Categories

Resources