不同web服务器之间采用http发送请求以及处理返回数据
1:数据采用json格式传送。
2:双方共同约定好的密钥进行加解密。
3:发送请求时,加密后再采用encode编码,处理请求时,不需要decode编码直接解密即可。
步骤:
1:构造json数据格式
String custId = "123456789";
String partnerCode = "test";
User user = new User(custId,partnerCode);
JSONObject jsonObject = JSONObject.fromObject(user); //构造json格式数据
2: 加密
DiscuzAuthcode uc = new DiscuzAuthcode();
String decString = uc.uc_authcode(jsonObject.toString(), "ENCODE", "9876543210");
LOGGER.info("加密后:" + decString);
3:发送到其它web服务器的http请求
String connURL = "http://192.168.0.118/taobao/service/xxxxxx.do";
String result = HttpConnection.httpConnection.sendAndReceiveHttpRequest(connURL, "param", decString);
发送http请求大概步骤
3.1.1 根据connURL获取http连接对象
/**
* 获取HTTP链接
*
* @return
*/
private HttpURLConnection getHttpConnection(String connURL) {
logger.info("request URL:" + connURL);
URL url = null;
HttpURLConnection httpConn = null;
try {
url = new URL(connURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestProperty("Proxy-Connection", "Keep-Alive");
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
HttpURLConnection.setFollowRedirects(true);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setUseCaches(false);
return httpConn;
} catch (MalformedURLException e) {
logger.info("", e);
} catch (IOException e) {
logger.info("", e);
}
return null;
}
3.1.2:发送http请求并获取返回数据
public String sendAndReceiveHttpRequest(String connURL, String key, String value) {
HttpURLConnection httpConn = this.getHttpConnection(connURL);
InputStream in = null;
OutputStream out = null;
try {
if (null == httpConn) {
return null;
}
httpConn.connect();
out = httpConn.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeBytes(key + "=" + URLEncoder.encode(value, "UTF-8")); //写参数
dos.flush();
dos.close();
InputStream is = httpConn.getInputStream();
String str = null;
StringBuffer buffer = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
while ((str = br.readLine()) != null) {
buffer.append(str);
}
br.close();
String data = buffer.toString();
return StringUtils.isBlank(data) ? null :data;
} catch (Exception e) {
logger.error(e);
} finally {
try {
if (in != null){
in.close();
}
} catch (Exception e2) {}
try {
if (out != null) {
out.close();
}
} catch (Exception e2) {}
try {
if (httpConn != null) {
httpConn.disconnect();
}
} catch (Exception e2) {}
}
return null;
}
3.3.3: web服务器处理接受到的http请求
public ActionForward queryPoint(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String ciphertext = request.getParameter("param");
String respStr = "{resultCode: '2', resultDetail: '传入的参数不正确'}";
logger.info("queryPoint() - 密文:" + ciphertext);
response.setCharacterEncoding("UTF-8");
//处理请求的具体逻辑
logger.info("queryPoint() - " + respStr);
response.getWriter().write(respStr);
return null;
}
4: 处理请求返回信息,保存到DB
LOGGER.info("返回的信息:" + result);
jsonObject = JSONObject.fromObject(result); //解析json格式数据
String resultCode = jsonObject.getString("resultCode");
String resultDetail = jsonObject.getString("resultDetail");
//todo 保存返回信息到DB
5:补充:
用到的加密工具类DiscuzAuthcode.java和 发送请求的类HttpConnection.java
6:用到的jar文件
json-lib-2.2.3-jdk15.jar,json-taglib-0.4.1.jar,ezmorph-1.0.6.jar,commons-xxx.jar