首页 技术 正文
技术 2022年11月21日
0 收藏 433 点赞 4,246 浏览 2257 个字

背景:使用httpclient 的post请求进行登录,需要重定向登录,请求重定向后的地址

在httpclient中post请求不像get请求自己可以重定向,实现方式是 判断post请求返回码是否是302,如果是那么就获取传递过来的Location的地址,进行拼接,在进行一个get的请求

实现代码

public Map<String, String> doPost(String url, Map<String, String> map, String charset) {
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
String domain = "http://user.hqygou.com";
Map<String, String> returnmap = new HashMap<String, String>();
try {
httpClient = new SSLClient();
httpPost = new HttpPost(url);
// 设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
System.out.println("请求的参数为:" + elem.getKey() + ":" + elem.getValue());
}
if (list.size() > 0) {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
httpPost.setEntity(entity);
}
// 设置头部信息
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
HttpResponse response = httpClient.execute(httpPost);if (response != null) {
int code = response.getStatusLine().getStatusCode();
System.out.println("返回的code为:" + code);
if (code == 302) { #判断post的请求返回码
Header[] hr = response.getAllHeaders();
for (int i = 0; i < hr.length; i++) {
Header header1 = hr[i];
System.out.println("头部的所有内容:" + header1);
}
String hearder = response.getHeaders("Location")[0].toString().split(":")[1].trim(); #获取返回码中头部中location 就是重定向的地址
String redirecturl = domain + hearder; //需要和域名进行拼接
System.out.println("开始重定向,地址为:" + redirecturl);
cookies = response.getHeaders("Set-Cookie")[0].toString().split(":")[1].trim();
System.out.println("获取的cookie:" + cookies);
cookies = cookies.split(";")[0].trim();
httpGet(redirecturl, cookies); #get请求,把获取的cookie进行一个拼接
} else {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
returnmap.put("content", result);
returnmap.put("cookies", cookies);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return returnmap;
}

  运行入口

public static void main(String[] args) {
test post = new test();
String url = "http://xxx/login/index/checklogin";
Map<String, String> map = new HashMap<String, String>();
map.put("from", "xx");
map.put("username", "xx");
map.put("password", "xx");
post.doPost(url, map, "UTF-8");
}

  httpclient 中post请求重定向

注,后面这个200,是get请求时返回的内容,get请求可以查看另外一篇文章,http://www.cnblogs.com/chongyou/p/7808035.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845