Android - 连续 HttpPost 的问题
我正在 Android 中编写一个简单的 HttpClient,因为我需要连续发出各种 POST 请求。我首先执行 HttpGet,然后执行第一个 HttpPost。 我可以检索第一个 GET 和第一个 POST 的 HTML。但是,如果我执行新的 GET 或新的 POST,我会得到空白响应。为了澄清我的问题,我附上代码。
DefaultHttpClient httpclient = new DefaultHttpClient();
//FIRST GET TO ACCESS LOGIN MODULE
try {
HttpGet httpget = new HttpGet("https://site/link_to_access_the_login_form");
HttpResponse response = null;
try {
response = httpclient.execute(httpget);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
entity.consumeContent();
//FIRST POST TO ACCESS THE RESTRICTED AREA
HttpPost httpost = new HttpPost("https://site/login/login.do");
List <NameValuePair> nameValuePairs = new ArrayList <NameValuePair>(6);
nameValuePairs.add(new BasicNameValuePair("login", "uid"));
nameValuePairs.add(new BasicNameValuePair("password", "pwd"));
//additional params
httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
//entity.consumeContent();
try {
String responseTextPost1 = EntityUtils.toString(entity);
entity.consumeContent();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//SECOND POST TO ACCESS A LINK IN THE RESTRICTED AREA
httpost = new HttpPost("https://site/role/script.do");
List <NameValuePair> nameValuePairs6 = new ArrayList <NameValuePair>(6);
//Parameters...
httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs6, HTTP.UTF_8));
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Login form get: " + response.getStatusLine());
entity = response.getEntity();
try {
String responseTextPost2 = EntityUtils.toString(entity);
entity.consumeContent();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch..............
responseTextPost2 似乎是空白的。关于如何解决这个问题的建议?
I'm writing a simple HttpClient in Android because I need to make various POST requests in succession. I first do a HttpGet and than the fisrt HttpPost.
I can retrieve the HTML of the first GET and of the first POST. But if I do a new GET or a new POST I obtain a blank response. To clarify my problem I attach the code.
DefaultHttpClient httpclient = new DefaultHttpClient();
//FIRST GET TO ACCESS LOGIN MODULE
try {
HttpGet httpget = new HttpGet("https://site/link_to_access_the_login_form");
HttpResponse response = null;
try {
response = httpclient.execute(httpget);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
entity.consumeContent();
//FIRST POST TO ACCESS THE RESTRICTED AREA
HttpPost httpost = new HttpPost("https://site/login/login.do");
List <NameValuePair> nameValuePairs = new ArrayList <NameValuePair>(6);
nameValuePairs.add(new BasicNameValuePair("login", "uid"));
nameValuePairs.add(new BasicNameValuePair("password", "pwd"));
//additional params
httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
//entity.consumeContent();
try {
String responseTextPost1 = EntityUtils.toString(entity);
entity.consumeContent();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//SECOND POST TO ACCESS A LINK IN THE RESTRICTED AREA
httpost = new HttpPost("https://site/role/script.do");
List <NameValuePair> nameValuePairs6 = new ArrayList <NameValuePair>(6);
//Parameters...
httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs6, HTTP.UTF_8));
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Login form get: " + response.getStatusLine());
entity = response.getEntity();
try {
String responseTextPost2 = EntityUtils.toString(entity);
entity.consumeContent();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch..............
responseTextPost2 seems to be blank. Suggestions on how to manage this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于后代...
问题是 NameValuePairs 变量的名称。他们必须有相同的名字。所以都是“nameValuePairs”。
希望这有帮助!
For posterity...
The problem is the name of NameValuePairs variables. They must have the same name. So both "nameValuePairs".
Hope this helps!