java发送http请求实例
来源:互联网 发布:扬州mac维修 编辑:程序博客网 时间:2024/06/05 07:19
//请求端
@RequestMapping ("/confirmPrivacyInfo")
@ResponseBodypublic String confirmPrivacyInfo (HttpServletResponse response, HttpServletRequest request)
{
Map <String, Object> resultMap = new HashMap <String, Object> ();
String uuId = request.getParameter ("uuId");// 获取校验的唯一uuid
String jsonStr = "";
try
{
if (StringUtils.isEmpty (uuId))
{
resultMap.put ("message", "传入参数为空,授权失败");
resultMap.put ("flagCode", "false");
jsonStr = JSONObject.fromObject (resultMap).toString ();
ResponseWriteUtil.writeJson (jsonStr, response);
return null;
}
// 通过uuid查询用户认证信息
PrivacyInfoRedis privacyInfo = privacyInfoRedisService.queryPrivacy (uuId);
if (null != privacyInfo)
{
// 校验隐私认证信息是否过期
boolean validateFlag = privacyInfoRedisService.isValidPrivacy (uuId);
if (validateFlag)
{
// 校验用户session中
UserDto userDto = (UserDto) request.getSession ().getAttribute (LoginConstants.LOGIN_USER);//
if (null == userDto)
{
resultMap.put ("message", "认证信息校验失败,授权失败");
resultMap.put ("flagCode", "false");
}
else
{
// 获取phm用户的登陆信息
// 比较用户认证信息及登陆信息是否一致
String idCard = userDto.getPortalUserInfo ().getIdentification ();
if (idCard.equals (privacyInfo.getIdCard ()))
{
// 认证成功
privacyInfo.setValidateFlag (true);
privacyInfoRedisService.addPrivacy (uuId, privacyInfo);// 将认证信息的状态改为ture
String phpPrvacyUrl = PropertiesUtils.getPropertiesFromPlatform ("php", "php.pushlet.url");
// 设置请求参数
String senUrl = phpPrvacyUrl + "/privacy/pushletPrivacy.do";
Map <String, Object> postMap = new HashMap <String, Object> ();
postMap.put ("uuId", uuId);
// 设置HttpPost请求
CloseableHttpClient httpClient = HttpClients.createDefault ();
HttpPost httpPostMethod = new HttpPost (senUrl);
// 解决中文乱码问题
String paramStr = JSONObject.fromObject (postMap).toString ();
StringEntity entity = new StringEntity (paramStr, "utf-8");
entity.setContentEncoding ("UTF-8");
httpPostMethod.setEntity (entity);
HttpResponse httpResponse = httpClient.execute (httpPostMethod);
// 请求发送成功,并得到响应
if (httpResponse.getStatusLine ().getStatusCode () == 200)
{
HttpEntity enrity = httpResponse.getEntity ();//获取请求结果
String result=EntityUtils.toString (enrity);resultMap.put ("message", "授权成功");
resultMap.put ("flagCode", "true");
}
else
{
_logger.error ("授权失败:url请求失败:" + httpResponse.getStatusLine ());
resultMap.put ("message", "授权失败");
resultMap.put ("flagCode", "false");
}
resultMap.put ("message", "授权成功");
resultMap.put ("pushletUrl", phpPrvacyUrl);
resultMap.put ("flagCode", "true");
}
else
{
privacyInfoRedisService.deletePrivacy (uuId);
resultMap.put ("message", "认证信息校验失败,授权失败");
resultMap.put ("flagCode", "false");
}
}
}
else
{
privacyInfoRedisService.deletePrivacy (uuId);
resultMap.put ("message", "认证信息过期,授权失败");
resultMap.put ("flagCode", "false");
}
}
else
{
resultMap.put ("message", "未查询到认证信息,授权失败");
resultMap.put ("flagCode", "false");
}
}
catch (Exception e)
{
resultMap.put ("message", "授权失败");
resultMap.put ("flagCode", "false");
_logger.error ("授权失败" + e.getMessage ());
}
jsonStr = JSONObject.fromObject (resultMap).toString ();
ResponseWriteUtil.writeJson (jsonStr, response);
return null;
}
//接受端
@RequestMapping (value = "/pushletPrivacy", method = RequestMethod.POST)
@ResponseBody
public String pushletPrivacy (@RequestBody String requestBody, HttpServletResponse response,
HttpServletRequest request)
{
String uuId = "";// 查询用户隐私授权信息唯一标识
try
{
JSONObject jsonObject = JSONObject.fromObject (requestBody);
uuId = jsonObject.getString ("uuId");
request.setCharacterEncoding ("utf-8");
response.setCharacterEncoding ("utf-8");
Event event = Event.createDataEvent ("/validate/privacy");
if (StringUtils.isNotBlank (uuId))
{
PrivacyInfoRedis privacyInfo = privacyInfoRedisService.queryPrivacy (uuId);// 查询redis缓存
if (null != privacyInfo && privacyInfo.isValidateFlag ())
{
event.setField ("validateFlag", "true");
if (StringUtils.isNotBlank (uuId))
{
event.setField ("uuId", uuId);
}
if (StringUtils.isNotBlank (privacyInfo.getUserName ()))
{
String userName = new String (privacyInfo.getUserName ().getBytes ("UTF-8"), "ISO-8859-1");
event.setField ("userName", userName);
}
if (StringUtils.isNotBlank (privacyInfo.getIdCard ()))
{
event.setField ("idCard", privacyInfo.getIdCard ());
}
}
else
{
event.setField ("validateFlag", "false");
}
}
else
{
event.setField ("validateFlag", "false");
}
// 将reids缓存的认证数据进行删除
privacyInfoRedisService.deletePrivacy (uuId);
String path = request.getSession ().getServletContext ().getRealPath ("");
if (StringUtils.isNotBlank (uuId))
{
File f = new File (path + File.separator + "images" + File.separator + uuId + ".jpg");
if (f.exists ())
{
f.delete ();
}
}
// 将消息推送到web客户端
Dispatcher.getInstance ().multicast (event);
}
catch (Exception e)
{
_logger.error ("查询认证信息服务失败:" + e.getMessage ());
}
return null;
}
- java发送http请求实例
- java 发送http请求
- JAVA发送HTTP请求
- Java发送Http请求
- java 发送http请求
- Java发送http请求
- java发送http请求
- java发送http请求
- java发送http请求
- java发送http请求
- java 发送http请求
- java发送http请求
- java发送http请求
- Java发送HTTP请求
- java发送http请求
- java发送http请求
- java发送http请求
- Java发送http请求
- Android 山寨机进入Recovery界面进行刷机、双清
- 百度云(BAE)文件上传目录问题-yellowcong
- JSP入门(三)
- 台阶问题
- 一维数组之求一个数组所有元素之和与平均值,并写出大于平均值的元素与其对应的下标。
- java发送http请求实例
- angularjs ng-if ng-show ng-hide区别
- [HEOI2005]BZOJ 4029定价
- eclipse版本mars.2 release 4.5.2本地安装SVN插件---------subclipse
- LeetCode
- Swift 3 点击屏幕任意位置隐藏键盘
- C语言重点知识复习1
- 使用下面这些工具和技巧,帮你快速开发一个Android App
- 几个java小问题代码(大神不喜勿喷)