Android-连续HttpPost的问题
我正在用Android编写一个简单的HttpClient,因为我需要连续发出各种POST请求。我首先做一个HttpGet,然后再做第一个HttpPost。 我可以检索第一个GET和第一篇POST的HTML。但是如果我做了一个新的GET或者一个新的帖子,我会得到一个空白的回复。为了澄清我的问题,我附上了代码Android-连续HttpPost的问题,android,connection,httpclient,Android,Connection,Httpclient,我正在用Android编写一个简单的HttpClient,因为我需要连续发出各种POST请求。我首先做一个HttpGet,然后再做第一个HttpPost。 我可以检索第一个GET和第一篇POST的HTML。但是如果我做了一个新的GET或者一个新的帖子,我会得到一个空白的回复。为了澄清我的问题,我附上了代码 DefaultHttpClient httpclient = new DefaultHttpClient(); //FIRST GET TO ACCESS LOGIN MOD
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..............
DefaultHttpClient-httpclient=newdefaulthttpclient();
//首先进入登录模块
试一试{
HttpGet HttpGet=新的HttpGet(“https://site/link_to_access_the_login_form");
HttpResponse响应=null;
试一试{
response=httpclient.execute(httpget);
}捕获(客户端协议例外e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
HttpEntity=response.getEntity();
System.out.println(“登录表单get:+response.getStatusLine());
entity.consumercontent();
//进入禁区的第一个哨所
HttpPost HttpPost=新的HttpPost(“https://site/login/login.do");
List nameValuePairs=新的ArrayList(6);
添加(新的BasicNameValuePair(“登录”、“uid”);
添加(新的BasicNameValuePair(“密码”、“pwd”);
//附加参数
setEntity(新的UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
试一试{
response=httpclient.execute(httpost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
entity=response.getEntity();
System.out.println(“登录表单get:+response.getStatusLine());
//entity.consumercontent();
试一试{
String responseTextPost1=EntityUtils.toString(实体);
entity.consumercontent();
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//进入禁区链接的第二个帖子
HttpPost=新的HttpPost(“https://site/role/script.do");
列表名称ValuePairs6=新的ArrayList(6);
//参数。。。
setEntity(新的UrlEncodedFormEntity(nameValuePairs6,HTTP.UTF_8));
试一试{
response=httpclient.execute(httpost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
System.out.println(“登录表单get:+response.getStatusLine());
entity=response.getEntity();
试一试{
String responseTextPost2=EntityUtils.toString(实体);
entity.consumercontent();
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}抓住。。。。。。。。。。。。。。
responseTextPost2似乎为空。关于如何处理这一问题的建议?供子孙后代参考
问题是NameValuePairs变量的名称。他们必须有相同的名字。所以这两个“名称-价值对”
希望这有帮助 HttpParams HttpParams=new BasicHttpParams();
HttpParams httpParams=new BasicHttpParams();
DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
//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..............
DefaultHttpClient httpclient=新的DefaultHttpClient(httpParams);
//首先进入登录模块
试一试{
HttpGet HttpGet=新的HttpGet(“https://site/link_to_access_the_login_form");
HttpResponse响应=null;
试一试{
response=httpclient.execute(httpget);
}捕获(客户端协议例外e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
HttpEntity=response.getEntity();
System.out.println(“登录表单get:+response.getStatusLine());
entity.consumercontent();
//进入禁区的第一个哨所
HttpPost HttpPost=新的HttpPost(“https://site/login/login.do");
List nameValuePairs=新的ArrayList(6);
添加(新的BasicNameValuePair(“登录”、“uid”);
添加(新的BasicNameValuePair(“密码”、“pwd”);
//附加参数
setEntity(新的UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
试一试{
response=httpclient.execute(httpost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
entity=response.getEntity();
System.out.println(“登录表单get:+response.getStatusLine());
//entity.consumercontent();
试一试{
String responseTextPost1=EntityUtils.toString(实体);
entity.consumercontent();
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//进入禁区链接的第二个帖子
HttpPost=新的HttpPost(“https://site/role/script.do");
列表名称ValuePairs6=新的ArrayList(6);
//参数。。。
setEntity(新的UrlEncodedFormEntity(nameValuePairs6,HTTP.UTF_8));
试一试{
response=httpclient.execute(httpost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(例外)