java内部发送http请求并取得返回结果,修改response的cookie
2015-12-30 18:43
561 查看
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("缓存信息丢失"); } }
相关文章推荐
- DefaultHttpClient is deprecated
- linux网络编程常用头文件
- GET和POST是HTTP的两个常用方法
- 记一次计算机网络工程实验(2) 配置交换机
- Http-用getInputStream()或者getParameterMap()获得Post请求的数据
- IBatis.Net 日志 http://www.cnblogs.com/firstyi/archive/2007/08/22/865028.html
- TCP/IP模型各个层次的功能和协议
- VMware® Workstation 虚拟网络管理
- HttpClient的Post和Get各种方法的工具类
- Https的握手协议
- 四谈iOS抓包:Mac下好用的HTTP/HTTPS抓包工具Charles
- HttpClient的Post和Get方法
- android http协议详细
- 三天快速制作易语言防破解网络验证注册码系统
- Android 网络请求详解
- 根据网络图片下载保存到新路径(包括本地)
- HTTPS、SPDY、HTTP/2 对比
- 第九章网络设备文件管理
- 使用JAVA实现Socket通信,TCP、UDP简析。
- OkHttp 源码分析