HttpClient4.2.1使用问题

恋小云 重庆交通大学 Java开发  2017-11-17 10:33:28
我是新手,客户端发送post请求到服务端,结果返回null,各位大神们帮忙看看客户端发送请求的代码有问题吗.?

public static OnlineServerResult onlineCompare(String name, String cardno, Integer businessTypes, 
String picNameR, String picNameT, OnlineServerConfig onlineServerConfig)
throws IOException {
lock.writeLock().lock();
String transactionID = getTransactionId(onlineServerConfig.getSourceCode());
lock.writeLock().unlock();
Logger logger = Logger.getLogger("comparelog");
logger.info("本次请求流水号transactionID:" + transactionID);
JsonObject reqParam = new JsonObject();
reqParam.addProperty("busiCode", onlineServerConfig.getBusiCode());
reqParam.addProperty("sourceCode", onlineServerConfig.getSourceCode());
reqParam.addProperty("targetCode", onlineServerConfig.getTargetCode());
reqParam.addProperty("version", onlineServerConfig.getVersion());
reqParam.addProperty("transactionID", transactionID);

JsonObject reqInfo = new JsonObject();
reqInfo.addProperty("userName", onlineServerConfig.getUserName());
reqInfo.addProperty("password", onlineServerConfig.getPassword());
reqInfo.addProperty("billId", "");
reqInfo.addProperty("custName", name);
reqInfo.addProperty("custCertNo", cardno);
reqInfo.addProperty("busiType", "1");
String reqInfoStr = null;
try {
RealNameMsDesPlus ms = new RealNameMsDesPlus();
reqInfoStr = ms.encrypt(reqInfo.toString());
} catch (Exception e) {
logger.error("请求json参数加密失败", e);
e.printStackTrace();
return null;
}
logger.info("reqInfo:" + reqInfo);
reqParam.addProperty("reqInfo", reqInfoStr);

JsonObject images = new JsonObject();
images.addProperty("picNameR", picNameR);
images.addProperty("picNameT", picNameT);
reqParam.add("images", images);
//json参数
JsonObject param = new JsonObject();
param.add("reqParam", reqParam);

logger.info("向在线公司发送请求参数:" + param);
String result = HttpUtil.doPost(onlineServerConfig.getUrl(), param.toString(),"application/json", "utf-8");
logger.info("在线公司响应json:" + result);
return new Gson().fromJson(result, OnlineServerResult.class);
}


HttpUtil.java
public static String doPost(String url, String jsonData, String contentType,  String charset) 
throws IOException {
HttpClient httpClient = getHttpClient();
HttpPost httpPost = new HttpPost(url);
//body参数
StringEntity s = new StringEntity(jsonData);
s.setContentEncoding(charset);
if (contentType != null && !"".equals(contentType)) {
s.setContentType(contentType);//发送数据需要设置contentType
}
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, charset);
httpPost.releaseConnection();
return result;
}
...全文
113 1 点赞 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
奈羽 2019-11-19
RealNameMsDesPlus 请问一下这个是那个jar包的类
  • 打赏
  • 举报
回复
相关推荐
发帖
Java
加入

4.8w+

社区成员

Java相关技术讨论
申请成为版主
帖子事件
创建了帖子
2017-11-17 10:33
社区公告
暂无公告