Using StringEntity to send POST Request in Android?

Go To StackoverFlow.com

0

I'm using the exact same code in 2 programs, except for the storing of the POST data. In the first method (the one that works) I use NameValuePair, and in the other, I use StringEntity (doesn't work, but I also don't use any encoding). The reason I don't use encoding is because it completely messes up my String. The code with the StringEntity won't work though... Here's the code:

public static String sendNamePostRequest(String urlString, String nameField) {

    HttpClient client = new DefaultHttpClient();

    HttpPost post = new HttpPost(urlString);

    StringBuffer sb = new StringBuffer();

    try {

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

        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 code does send the request (sniffed with wireshark to check), but doesn't return the appropriate HTML data. Any reason why this might be?

2012-04-04 23:19
by ZimZim
Did you try printing out what was the outgoing URL? This code will send the request, but if the URL is not correct, you won't get the HTML data that you're expectin - androidnoob 2012-04-04 23:42
The URL is correct. There are 2 reasons to prove this: 1. I'm using the exact same URL in a stock Java applications which sends POST Requests as well, and it works perfectly there. 2. It actually gives me the URL data, just the one before the post request instead of after.. - ZimZim 2012-04-04 23:46


0

Instead of using:

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

BufferedReader br = new BufferedReader(new InputStreamReader(
                entity.getContent()));

Remove HttpEntity entity = response.getEntity(); and do this:

HttpResponse response = client.execute(post);
BufferedReader br = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent(), "UTF-8"));

For NameValuePair, check this link

2012-04-04 23:55
by androidnoob
Doesn't that just encode the received data from the server? My problem is that my POST request isn't processed correctly... I still do get HTML data, it's just the regular page's HTML data instead of the data I should get after the page/server has processed my request.. - ZimZim 2012-04-04 23:59
I would suggest you check your URL again, because Android Java works a bit differently from stock Java, hence your code needs to be tweaked a bit. If possible, please paste the URL that was produced by the stock Java code and the one above. I'm guessing that the way Android encodes POST request is a little different from stock Jav - androidnoob 2012-04-05 00:03
The site is http://ed.cupweb6.nl, it's a Dutch school site which my little brother uses. But I used this exact method in another program, only there the sent data string was put into NameValuePairs but that doesn't work here.. - ZimZim 2012-04-05 00:36
Check my edited answe - androidnoob 2012-04-05 00:43
That only adds problems since NameValuePair doesnt work with this (in this case - ZimZim 2012-04-05 01:11
Can you post your code which uses NameValuePair? Also, check my edited answe - androidnoob 2012-04-05 01:22
Ads