我在 2 个程序中使用完全相同的代码,除了存储 POST 数据。在第一种方法(有效的方法)中,我使用 NameValuePair,在另一种方法中,我使用 StringEntity(不起作用,但我也不使用任何编码)。我不使用编码的原因是因为它完全弄乱了我的字符串。不过,带有 StringEntity 的代码将不起作用......这是代码:
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();
}
此代码确实发送了请求(用wireshark 嗅探以检查),但不返回适当的HTML 数据。这可能是什么原因?