背景:使用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请求重定向的更多相关文章

  1. HttpClient中post请求http、https示例

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...

  2. httpClient解决post请求重定向的问题

    import com.dadi.saas.util.HTTPUtils; import org.apache.commons.httpclient.Header; import org.apache. ...

  3. Servlet中的请求转发和重定向

    跳转和重定向 有的时候客户端请求到达服务端后需要对请求重新转发到其它Servlet甚至别的服务器,这就需要跳转和重定向. 区别 一般来说,跳转是服务器内部跳转,例如将请求从一个Servlet转发给另外 ...

  4. httpClient 中的post或者get请求

    httpClient相对于java自带的请求功能更加强大,下面就以例子的形式给出: //HttpClient Get请求 private static void register() { try { ...

  5. SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例

     要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...

  6. Servlet中请求重定向和请求转发和include

    响应的重定向 response.sendRedirect("ShowMSgSerlet1");//请求重定向 会将后面的浏览器的url改变. 请求转发 RequestDispatc ...

  7. [javaweb]javaweb中HttpServletResponse实现文件下载,验证码和请求重定向功能

    HttpServletResponse web服务器接受到客户端的http请求之后,针对这个请求,分别创建一个代表请求的httpServletRequest和代表响应的HttpServletRespo ...

  8. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  9. HttpClient如何解决302重定向问题

    最近的接口测试,发现接口地址报302错误,通过上网搜索,发现问题所在,解决办法是需要请求重定向后的URI. package com.btv; import org.apache.http.Header ...

随机推荐

  1. MySQL SQL_MODE详解

    http://blog.itpub.net/29773961/viewspace-1813501/

  2. apache 与php的安装

    1 系统环境与软件   1 php5.5.6 下载链接:http://windows.php.net/download/#php-5.5 推荐 V11 x64,也就是64bit的. 2 apache2 ...

  3. mapreduce统计总数

    现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1. buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t ...

  4. 移动测试之appium+python 简单例子(五)

    # coding=utf-8 from appium import webdriver import time import unittest import os import HTMLTestRun ...

  5. python_案例综合:教材记录管理

    class Book(): def __init__(self,ISBN,Ftile,Author,Publisher): self.ISBN = ISBN self.Ftile = Ftile se ...

  6. notepad++查看Log

    安装步骤:打开notepad++,插件→ Plugin Manager→ Show Plugin Manager→ Avaliable→ 等待插件列表刷新出来后,选中Android Logger插件→ ...

  7. [转]分享20佳好玩的 jQuery 游戏

    本文转自:http://www.cnblogs.com/lhb25/archive/2011/04/17/2001089.html jQuery是时下最流行的 JavaScript 库.现在,除了HT ...

  8. [转]jquery的ajax交付时“加载中”提示的处理方法

    本文转自:http://www.educity.cn/wenda/77121.html jquery的ajax提交时“加载中”提示的处理方法    方法1:使用ajaxStart方法定义一个全局的“加 ...

  9. QML 程序运行效率

    同样的程序,在 Windows 下面启动时非常慢,而在 Linux 上启动时很快,一方面是因为 qml 界面的创建耗时不同,另一方面是因为读取文件的用时相差太大导致. On Linux 在 Linux ...

  10. POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

    Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...