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. 私有云存储搭建(owncloud)

    第一步.搭建LAMP(基于linux7.1.1503) 1 配置yum(网络加本地,下面为网络) [vault.centos.org_7.1.1503_os_x86_64_] name=added f ...

  2. ubuntu-14.10下,安装gcc交叉编译器遇到问题及解决方法

    一 下载gcc-arm-none-eabi-4_9 安装成功后上报错误: ./gcc-arm-none-linux-gnueabi-gcc: No such file or directory 网上查 ...

  3. [CSS3]学习笔记-CSS基本样式讲解

    1.CSS样式-背景 CSS运行应用纯色作背景,也允许使用背景图像创建相当复杂的效果 <!DOCTYPE html> <html> <head lang="en ...

  4. [html5] 学习笔记-SVG

    1.SVG介绍:什么是SVG? 1)SVG指可伸缩矢量图形(Scalable Vector Graphics) 2)SVG用来定义用于网络的基于矢量的图形 3)SVG使用XML格式定义图形 4)SVG ...

  5. [html5]学习笔记一 新增的主题结构元素

    html5 新增的主体结构元素有:article,section,nav,aside,time,pubdate元素. 1. article元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引 ...

  6. .net 网站应对压力的一些方案总结

    开年比较空,抽时间写个博文,总结下自己工作里的一些应对网站访问压力的技术方案. 自己项目现在大概一天50W的pv.已从前端到后端的顺序总结下自己用的一些方案. 一. 前端页面: 1.首先减少资源的大小 ...

  7. node将excel内容转json

    小颖分享的这个方法,前提是你已经安装了node,如果大家不知道自己是否安装过node可以打开cmd,然后执行:node -v,如果安装过,你会看到你安装的node版本号,如果没有安装请先安装node. ...

  8. phpcmsV9静态页面替换动态步骤

    1.先在www目录下找到 phpcms + install_package + phpcms + templates在templates 文件夹里创建个自己的文件夹我弄得是 ceshi 文件夹,在 c ...

  9. 如何居中div?如何居中一个浮动元素?

    如何居中div? 给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto; } 如何居中一个浮动元素? <!DOCTYPE ...

  10. <input/>标签在 苹果浏览器中 默认的有圆角

    解决方法: input{ border-radius:0; -webkit-border-radius:0; }