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. CMD修改IP地址

    在操作系统下,我们可以使用"本地连接"的属性来修改IP地址,但是如果我们要在多个IP地址之间切换,使用这种方法未免过于麻烦.我们可以使用NETSH命令来添加,相当简便.使用DOS修 ...

  2. [个人翻译]GitHub指导文件(GitHub Guides[Hello World])

    [个人翻译]GitHub指导文件(GitHub Guides[Hello World]) Mirage_j个人翻译,欢迎转载,最好标明出处http://www.cnblogs.com/mirageJ/ ...

  3. Windows服务的创建、安装、卸载

    1.新建Window服务项目 2.添加安装配置文件 3.serviceProcessInstaller1右键属性,设置Account属性为LocalSystem. serviceInstaller1右 ...

  4. 神秘的ApplicationPoolIdentity再也不用妈妈担心程序池安全了

    在IIS 7和IIS 7.5中,我们可以为应用程序池设置一个特殊的Identity(用户标识):ApplicationPoolIdentity. 那么这个标识到底是什么意思?它是具体什么身份呢?这一讲 ...

  5. Windows下搭建HTTP/HTTPS服务器及测试过程

    1 安装Apache http://www.apachehaus.com/cgi-bin/download.plx 选择合适的版本下载 本次下载的是 Apache 2.4.x VC14 Apache版 ...

  6. C语言的函数类型

    C语言的函数类型与返回值类型不一致时出现,是以函数类型为标准; 而如果在java与c#语言中上述情况是编译错误的;

  7. TypeScript-01-变量、基本类型和运算符

    基本类型 基本类型有boolean.number.string.array.void.所有类型在TypeScript中,都是一个唯一的顶层的Any Type 类型的自类型.any关键字代表这种类型. ...

  8. PhotoshopCC 如何使用动作文件ATN

    非常感谢公司的前端同事,今早给我推荐了一个很好用的插件 atn ,下面简单的总结下 导入 atn 插件的方法: 打开 photoshop 或者 photoshopCC 软件→点击 窗口菜单→找到 动作 ...

  9. 【Zookeeper】源码分析之请求处理链(三)

    一.前言 在分析了PrepRequestProcessor处理器后,接着来分析SyncRequestProcessor,该处理器将请求存入磁盘,其将请求批量的存入磁盘以提高效率,请求在写入磁盘之前是不 ...

  10. ARP攻击

    ARP攻击,是针对以太网地址解析协议(ARP)的一种攻击技术,就是通过伪造IP地址和MAC地址实现ARP欺骗,能够在网络中产生大量的ARP通信量使网络阻塞.此种攻击可让攻击者取得局域网上的数据封包甚至 ...