httpclient 中post请求重定向
背景:使用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");
}

注,后面这个200,是get请求时返回的内容,get请求可以查看另外一篇文章,http://www.cnblogs.com/chongyou/p/7808035.html
httpclient 中post请求重定向的更多相关文章
- HttpClient中post请求http、https示例
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...
- httpClient解决post请求重定向的问题
import com.dadi.saas.util.HTTPUtils; import org.apache.commons.httpclient.Header; import org.apache. ...
- Servlet中的请求转发和重定向
跳转和重定向 有的时候客户端请求到达服务端后需要对请求重新转发到其它Servlet甚至别的服务器,这就需要跳转和重定向. 区别 一般来说,跳转是服务器内部跳转,例如将请求从一个Servlet转发给另外 ...
- httpClient 中的post或者get请求
httpClient相对于java自带的请求功能更加强大,下面就以例子的形式给出: //HttpClient Get请求 private static void register() { try { ...
- SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例
要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...
- Servlet中请求重定向和请求转发和include
响应的重定向 response.sendRedirect("ShowMSgSerlet1");//请求重定向 会将后面的浏览器的url改变. 请求转发 RequestDispatc ...
- [javaweb]javaweb中HttpServletResponse实现文件下载,验证码和请求重定向功能
HttpServletResponse web服务器接受到客户端的http请求之后,针对这个请求,分别创建一个代表请求的httpServletRequest和代表响应的HttpServletRespo ...
- 【JAVA】通过HttpClient发送HTTP请求的方法
HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...
- HttpClient如何解决302重定向问题
最近的接口测试,发现接口地址报302错误,通过上网搜索,发现问题所在,解决办法是需要请求重定向后的URI. package com.btv; import org.apache.http.Header ...
随机推荐
- Java对象排序两种方法
转载:https://blog.csdn.net/wangtaocsdn/article/details/71500500 有时候需要对对象列表或数组进行排序,下面提供两种简单方式: 方法一:将要排序 ...
- 单例模式+volatile禁止指令重排序
单例模式: 单例,顾名思义就是只能有一个.不能再出现第二个.就如同地球上没有两片一模一样的树叶一样. 在这里就是说:一个类只能有一个实例,并且整个项目系统都能访问该实例. 单例模式共分为两大类: 懒汉 ...
- Murano Weekly Meeting 2015.09.15
Meeting time: 2015.September.15th 1:00~2:00 Chairperson: Serg Melikyan, PTL from Mirantis Meeting s ...
- nyoj 1216——整理图书 CF 229D—— Towers——————【dp+贪心】
整理图书 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 小明是图书鹳狸猿,他有很多很多的书堆在了一起摆在了架子上,每摞书是横着放的,而且每摞书是订好的 是一个整体, ...
- Window WindowManager 和WindowManager.LayoutParams
<一> Window window是android中的窗口,表示顶级窗口的意思,也就是主窗口,它有两个实现类, PhoneWindow和MidWindow,我们一般的activity对应的 ...
- [转] Java集合类详解
集合类说明及区别Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap ...
- Memcache安装、配置与学习
基于现在大多网站数据很多,由于页面性能问题我们都开始对站点使用缓存进行性能优化 Memcache扩展下载:http://windows.php.net/downloads/pecl/releases/ ...
- .net iis6中配置伪静态
1.右键点击 要设置网站的网站 2.属性 ——>主目录 ——>配置——> 3.如右侧窗口,找到 .aspx 扩展名——>编辑——>复制 可执行文件的路径——>关闭 ...
- MVC在页面View上获取当前控制器名称、Action名称以及路由参数
有时候在封装MVC通用控件时需要在页面上获取这些数据. 用以下方法即可: //获取控制器名称: ViewContext.RouteData.Values["controller"] ...
- HTTP的GET和POST格式解析
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yc0188/archive/2009/10/29/4741871.aspx HTTP报文是面向文本的,报文中的每一个字 ...