今天上班搞了一个第三方接口测试的问题,因为之前没做过没有使用过HttpClient这个包,所以搞了好久
这边是要传递6个参数,使用md5加密,并附带时间戳,接受的是一个json类型
首先要引入依赖
1 <dependency> 2 <groupId>org.apache.httpcomponents</groupId> 3 <artifactId>httpclient</artifactId> 4 <version>4.5.2</version> 5 </dependency> 6 7 <dependency> 8 <groupId>org.apache.httpcomponents</groupId> 9 <artifactId>httpclient-cache</artifactId> 10 <version>4.5</version> 11 </dependency> 12 13 <dependency> 14 <groupId>org.apache.httpcomponents</groupId> 15 <artifactId>httpmime</artifactId> 16 <version>4.3.2</version> 17 </dependency>
package com.Test; import java.io.*; import java.math.BigInteger; import java.net.*; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONObject; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; @Slf4j public class appaddTest { public static final String ADD_URL = "url"; public static final String cmd = "cmd"; public static final String appid = "appid"; public static final String appkey= "appkey"; public static final String param = ""; public static final String comkeyid = "comkeyid"; public static final String comkey = "comkey"; public static final String com = "com"; public static final String part = ""; public static void main(String[] args) { // String result = sendGet(ADD_URL,param); // System.out.println(result); // appadd(); String result = sendPost(); System.out.println(result); } public static void appadd() { try { URL url = new URL(ADD_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8"); connection.connect(); PrintWriter out = new PrintWriter(connection.getOutputStream()); JSONObject obj = new JSONObject(); //设置cmd obj.element("cmd",cmd ); //设置appid obj.element("appid", appid); //设置param obj.element("param",param); //upr设置 com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(); json.put("comkeyid",comkeyid); json.put("com",com); json.put("part",part); String stamp = (new Date().getTime()+"").substring(0,10); String md = stamp +comkey+com+part; String md2Md5 = md5digest(md); System.out.println("加密前"+md); System.out.println("加密后"+md2Md5); json.put("md",md2Md5); obj.element("upr", json.toJSONString()); //设置时间戳 obj.element("stamp", stamp); //设置md String MD = param + stamp+ json.toJSONString()+cmd+appkey; System.out.println("加密前"+MD); String MD2Md5 = md5digest(MD); System.out.println("加密后"+MD2Md5); obj.element("md",MD2Md5); out.print(obj.toString()); log.info(obj.toString()); out.flush(); out.close(); System.out.println(" connection.getInputStream():"+ connection.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } log.info(String.valueOf(sb)); reader.close(); connection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static String md5digest(String str){ String md5Str = null; MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); md5.update(str.getBytes()); md5Str = new BigInteger(1, md5.digest()).toString(16); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return md5Str; } public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } public static String getParam(){ String upr =null; String paramUrl =null; com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(); json.put("part",part); json.put("comkeyid",comkeyid); json.put("com",com); String stamp = (new Date().getTime()+"").substring(0,10); String md = stamp +comkey+com+part; String md2Md5 = md5digest(md); json.put("md",md2Md5); System.out.println("加密前"+md); System.out.println("加密后"+md2Md5); try { upr = URLEncoder.encode(json.toString(),"UTF-8"); paramUrl= URLEncoder.encode(param,"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String MD = param + stamp+ json.toJSONString()+cmd+appkey; System.out.println("加密前"+MD); String MD2Md5 = md5digest(MD); System.out.println("加密后"+MD2Md5); String param = "cmd="+cmd+"&appid="+appid+"¶m="+paramUrl+"&upr="+upr+"&stamp="+stamp+"&md="+MD2Md5; System.out.println(ADD_URL+"?"+param); return param; } public static String sendPost(){ CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(ADD_URL); CloseableHttpResponse response = null; String resultString = ""; //得到upr com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(); json.put("comkeyid",comkeyid); json.put("com",com); json.put("part",part); String stamp = (new Date().getTime()+"").substring(0,10); String md = stamp +comkey+com+part; String md2Md5 = md5digest(md); System.out.println("加密前"+md); System.out.println("加密后"+md2Md5); json.put("md",md2Md5); //得到md String MD = param + stamp+ json.toJSONString()+cmd+appkey; System.out.println("加密前"+MD); String MD2Md5 = md5digest(MD); System.out.println("加密后"+MD2Md5); List<NameValuePair> paramList = new ArrayList<>(); paramList.add(new BasicNameValuePair("cmd",cmd )); paramList.add(new BasicNameValuePair("appid", appid)); paramList.add(new BasicNameValuePair("param",param)); paramList.add(new BasicNameValuePair("upr", json.toJSONString())); paramList.add(new BasicNameValuePair("stamp", stamp )); paramList.add(new BasicNameValuePair("md",MD2Md5)); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); httpPost.setEntity(entity); response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } return resultString; } } }