public Object userLogin(HttpServletRequest request, HttpServletResponse response, String email, String password,
String captcha) {
    
     //获取sessionId
String jsessionIdSt = getCookieStringByKey(request, "JSESSIONID"); if (StringUtils.isEmpty(jsessionIdSt)) {
return ResultVOUtil.retFailed("登录缓存信息为空");
} if (StringUtils.isNotBlank(jsessionIdSt)) { if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password) || StringUtils.isEmpty(captcha)) {
ResultVOUtil.retFailed("用户名/用户密码/验证码不能为空");
} // 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建请求方法实例
HttpPost httpPost = new HttpPost("http://www.test.com/user/login");
CloseableHttpResponse innerResponse = null;
HttpEntity entity = null; httpPost.addHeader(new BasicHeader("Cookie", "JSESSIONID=" + jsessionIdSt));
// 创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("email", email));
formparams.add(new BasicNameValuePair("password", password)); UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httpPost.setEntity(uefEntity);
         // 发送请求并接收response
innerResponse = httpclient.execute(httpPost);
         //解析response
entity = innerResponse.getEntity(); if (entity != null) { // 成功
String ssoResultSt = EntityUtils.toString(entity, CHAR_SET_UTF_8); JSONObject ssoResultJson = JSONObject.parseObject(ssoResultSt); String ssoData = ssoResultJson.getString("data");
Integer ssoCode = ssoResultJson.getInteger("code");
String ssoMsg = ssoResultJson.getString("msg"); if (ssoCode == null) {
return ResultVOUtil.retFailed("SSO登录返回状态为空");
} // 登录成功,返回码为预设的值
if (ssoCode.intValue() == 1) {
// response植入cookie
Header[] ssoResponseHeader = innerResponse.getHeaders("Set-Cookie"); if (ssoResponseHeader != null && ssoResponseHeader.length != 0) {
for (Header stepHeader : ssoResponseHeader) {
if (stepHeader != null) {
response.addHeader(stepHeader.getName(), stepHeader.getValue());
}
}
}
return ResultVOUtil.retSuccess(ssoData);
}
// 登录失败
else {
return ResultVOUtil.retFailed(ssoMsg);
} } else {
return ResultVOUtil.retFailed("登录端没有响应");
} } catch (ClientProtocolException protocolException) { logger.error(protocolException.getMessage(), protocolException); } catch (UnsupportedEncodingException uException) { logger.error(uException.getMessage(), uException); } catch (IOException ioException) { logger.error(ioException.getMessage(), ioException); } finally { // 关闭连接,释放资源
try {
if (innerResponse != null) {
innerResponse.close();
}
httpclient.close(); } catch (IOException e) {
logger.error(e.getMessage());
}
} return ResultVOUtil.retFailed("业务异常,导致登录失败"); } else {
return ResultVOUtil.retFailed("缓存信息丢失");
} }

java内部发送http请求并取得返回结果,修改response的cookie的更多相关文章

  1. java httpclient发送json 请求 ,go服务端接收

    /***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...

  2. java中发送http请求的方法

    package org.jeecgframework.test.demo; import java.io.BufferedReader; import java.io.FileOutputStream ...

  3. 用java代码发送http请求

    //发送post请求 PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL ...

  4. 从客户发送http请求到服务器返回http之间发生了什么

    由于我知识有限,可能会有模糊或者错误的地方,欢迎讨论与指正. 1.浏览器发出http请求 当用户访问一个url时,浏览器便会开始生成一个http请求. 首先获取http请求中所需要的参数,如url,c ...

  5. 对于java用发送http请求,请求内容为xml格式

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...

  6. java okhttp发送post请求

    java的httpclient和okhttp请求网络,构造一个基本的post get请求,都比py的requests步骤多很多,也比py的自带包urllib麻烦些. 先封装成get post工具类,工 ...

  7. java 模拟发送post请求测试

    方法一: HttpClient public void postTest(HttpServletRequest request,Integer type,String phone,String pas ...

  8. JMeter发送get请求并分析返回结果

    在实际工作的过程中,我们通常需要模拟接口,来进行接口测试,我们可以通过JMeter.postman等多种工具来进行接口测试,但是工具的如何使用对于我们来说并不是最重要的部分,最重要的是设计接口测试用例 ...

  9. 一个完整的用java客户端使用httpClient请求网页并返回的方法

    import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import ja ...

随机推荐

  1. com.atomikos.icatch.HeurHazardException: Heuristic Exception

    com.atomikos.icatch.HeurHazardException: Heuristic Exception: 删除Tomcat  bin文件夹下的spring.loglog4j.appe ...

  2. SQL 存储过程 触发器 事务

    一.存储过程 存储过程:就像函数一样的会保存在:数据库中-->可编程性 --> 存储过程 创建存储过程:create proc JiaFa --存储关键字proc @a int,      ...

  3. Eclipse / Intellij Idea配置Git+Maven+Jetty开发环境

    作者:鹿丸不会多项式 出处:http://www.cnblogs.com/hechao123  转载请先与我联系. 最近公司给加配了Mac,本想着花一个小时的时间搭好开发环境,最后全部弄好却用了一上午 ...

  4. 都能读懂的css3 3D变形效果

    css3 3D变形效果 CSS3 transform3D变形 transform的含义是:改变,使-变形:转换 三维变换使用基于二维变换的相同属性,如果您熟悉二维变换,你们发现3D变形的功能和2D变换 ...

  5. JS数组处理

    一.定义数组: 方法1 var myCars=new Array(); myCars[0]="Saab"; myCars[1]="Volvo"; myCars[ ...

  6. android开发过程中踩过的坑

    1) 4.X下 viewgroup 不一定会向下传递requestLayout,当onlayout的速度比较慢(比如子View比较复杂之类的原因),系统会跳帧!此时子View下层的view可能就不会再 ...

  7. checkinstall包的使用

    1. Checkinstall是个很有用的工具.当软件编译过后,Checkinstall能够帮助安装. 下面的命令是安装软件 ./configure make make install 但是用这种安装 ...

  8. C/C++面试之算法系列--去除数组中的重复数字

    去除数组中的重复数字 Sailor_forever  sailing_9806@163.com 转载请注明 http://blog.csdn.net/sailor_8318/archive/2008/ ...

  9. 360随身wifi无法使用临时解决方案大全

       360随身wifi在绝大多数情况下都是可以正常使用的,但在极少数系统或网络环境下可能会出现异常,如系统服务缺失.公司网络限制.少数校园网客户端限制等等:       360攻城师正在积极努力解决 ...

  10. 2011 Multi-University Training Contest 1 - Host by HNU

    A.A + B problem(待填坑) B.Cat VS Dog(二分图匹配) 喜欢cat和喜欢dog的人构成了二分图,如果两个人有冲突则连一条边,则问题转化为二分图最大点独立集问题.ans=n-最 ...