Android HTTP Post Request doesn't work, but stock Java does?

Go To StackoverFlow.com

0

So I have 2 codes which supposedly do the same. The one I'm using on Android however, returns the wrong HTML data. The stock Java one returns the correct data after sending the request. I have both codes here. Can you tell me (EVEN THOUGH I GAVE INTERNET PERMISSION TO ANDROID) why the Android one isn't working, while the stock Java one is working? This is the Android code:

EDIT: I FOUND THE FIX. If you're going to use a StringEntity to send such a String to the Server, you have to set the content to application/x-www-form-urlencoded. I've edited my code to show this:

public static String sendNamePostRequest(String urlString) {

    HttpClient client = new DefaultHttpClient();

    HttpPost post = new HttpPost(urlString);

    StringBuffer sb = new StringBuffer();

    try {
           StringEntity se = new StringEntity(
                "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE3NDM5MzMwMzRkZA%3D%3D&__EVENTVALIDATION=%2FwEWBAL%2B%2B4CfBgK52%2BLYCQK1gpH7BAL0w%2FPHAQ%3D%3D&_nameTextBox=John&_zoekButton=Zoek&numberOfLettersField=3"); 

        se.setContent("application/x-www-form-urlencoded");

        post.setEntity();

        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                entity.getContent()));
        String in = "";

        while ((in = br.readLine()) != null) {
            sb.append(in + "\n");
        }

        br.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return sb.toString();
}

This is the stock Java code:

public String sendNamePostRequest(String urlString) {

    StringBuffer sb = null;

    try {
        String data = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE3NDM5MzMwMzRkZA%3D%3D&__EVENTVALIDATION=%2FwEWBAL%2B%2B4CfBgK52%2BLYCQK1gpH7BAL0w%2FPHAQ%3D%3D&_nameTextBox=John&_zoekButton=Zoek&numberOfLettersField=3";

        // String data = "";

        URL requestUrl = new URL(urlString);

        HttpURLConnection conn = (HttpURLConnection) requestUrl
                .openConnection();

        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(data);
        dos.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));

        String in = "";
        sb = new StringBuffer();

        while ((in = br.readLine()) != null) {
            sb.append(in + "\n");
        }

        dos.close();
        br.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}
2012-04-04 23:57
by ZimZim
Specify what do you mean by "wrong HTML data - 207 2012-04-05 00:18
It means that I'm supposed to get back the HTML data of the page after it has processed the request. The stock Java code does that perfectly, but the Android code just returns the HTML page of the site without taking the POST request into account. Wireshark does see a POST from both the Android and Stock side though, with the exact same data String, but for some reason, he one sent from Java doesn't get processed.. - ZimZim 2012-04-05 00:39
In the android case you probably need to tell the StringEntity to set the content-type to application/x-www-form-urlencode - superfell 2012-04-05 00:57
I can think of that your entity string makes trouble. Try using new StringEntity("yourString", HTTP.UTF_8)207 2012-04-05 01:21


0

If the data is getting to the server, you might want to see what is happening there (logs, errors, exceptions, etc.) Other than that:

  • use can use HttpURLConnection, so you can have the exact same code
  • For HttpClient, not sure whey you are encoding the entity yourself. Use NameValuePair to set parameters, and HttpClient will encode them for you (correctly).
2012-04-05 03:43
by Nikolay Elenkov
Encoding the String only gives problems in Android. It says something about it not being base64 (probably because of the amount of _ and % and whatnot in the code. So I can't urlencode the String which sucks but I don't know if this is really the problem since it works without encoding in stock Java.. - ZimZim 2012-04-05 06:07
You do release that your 'stock Java' and Android code are fundamentally different, do you? One is using HttpURLConnection, the other HttpClient. Most probably, HttpClient has a stricter check, that is why you are getting an error on Android. Post the full stack trace, and try to find what part is wrongly encoded. Additionally, you shouldn't need to use VIEWSTATE from Android, that's for saving form state. Just set the parameters the serverside script needs (name, etc.) - Nikolay Elenkov 2012-04-05 06:23
Thanks for the help, but I just tried setContent("application/x-www-form-urlencoded"); and it immediately worked. I appreciate your help though - ZimZim 2012-04-05 07:15


0

You can use NameValuePair and UrlEncodedFormEntity:

List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair(KEY1, VALUE1));
nvps.add(new BasicNameValuePair(KEY2, VALUE2));
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
post.setEntity(p_entity);
2012-04-05 04:19
by Andrey P
Nope I can't. I've used this in other apps and it works just fine, but in this program it gives me trouble - ZimZim 2012-04-05 06:03
Ads