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?
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