1 package com.kdt.api; 2 3 import java.io.File; 4 import java.net.URLEncoder; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.TimeZone; 10 11 import org.apache.http.HttpResponse; 12 import org.apache.http.client.HttpClient; 13 import org.apache.http.client.methods.HttpGet; 14 import org.apache.http.client.methods.HttpPost; 15 import org.apache.http.entity.mime.HttpMultipartMode; 16 import org.apache.http.entity.mime.MultipartEntity; 17 import org.apache.http.entity.mime.content.ContentBody; 18 import org.apache.http.entity.mime.content.FileBody; 19 import org.apache.http.impl.client.DefaultHttpClient; 20 21 public class KdtApiClient { 22 private static final String Version = "1.0"; 23 24 private static final String apiEntry = "https://open.koudaitong.com/api/entry?"; 25 26 private static final String format = "json"; 27 28 private static final String signMethod = "md5"; 29 30 private static final String DefaultUserAgent = "KdtApiSdk Client v0.1"; 31 32 private String appId; 33 private String appSecret; 34 35 public KdtApiClient(String appId, String appSecret) throws Exception{ 36 if ("".equals(appId) || "".equals(appSecret)){ 37 throw new Exception("appId 和 appSecret 不能为空"); 38 } 39 40 this.appId = appId; 41 this.appSecret = appSecret; 42 } 43 44 public HttpResponse get(String method, HashMap<String,String> parames) throws Exception{ 45 String url = apiEntry + getParamStr(method, parames); 46 47 HttpClient client = new DefaultHttpClient(); 48 HttpGet request = new HttpGet(url); 49 request.addHeader("User-Agent", DefaultUserAgent); 50 51 HttpResponse response = client.execute(request); 52 return response; 53 } 54 55 public HttpResponse post(String method, HashMap<String, String> parames, List<String> filePaths, String fileKey) throws Exception{ 56 String url = apiEntry + getParamStr(method, parames); 57 58 HttpClient client = new DefaultHttpClient(); 59 HttpPost httppost = new HttpPost(url); 60 httppost.addHeader("User-Agent", DefaultUserAgent); 61 62 if(null != filePaths && filePaths.size() > 0 && null != fileKey && !"".equals(fileKey)){ 63 MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 64 for(int i = 0; i < filePaths.size(); i++){ 65 File file = new File(filePaths.get(i)); 66 ContentBody cbFile = new FileBody(file); 67 mpEntity.addPart(fileKey, cbFile); 68 } 69 70 httppost.setEntity(mpEntity); 71 } 72 73 System.out.println("executing request " + httppost.getRequestLine()); 74 HttpResponse response = client.execute(httppost); 75 76 return response; 77 } 78 79 public String getParamStr(String method, HashMap<String, String> parames){ 80 String str = ""; 81 try { 82 str = URLEncoder.encode(buildParamStr(buildCompleteParams(method, parames)), "UTF-8") 83 .replace("%3A", ":") 84 .replace("%2F", "/") 85 .replace("%26", "&") 86 .replace("%3D", "=") 87 .replace("%3F", "?"); 88 } catch (Exception e) { 89 e.printStackTrace(); 90 } 91 92 return str; 93 } 94 95 private String buildParamStr(HashMap<String, String> param){ 96 String paramStr = ""; 97 Object[] keyArray = param.keySet().toArray(); 98 for(int i = 0; i < keyArray.length; i++){ 99 String key = (String)keyArray[i]; 100 101 if(0 == i){ 102 paramStr += (key + "=" + param.get(key)); 103 } 104 else{ 105 paramStr += ("&" + key + "=" + param.get(key)); 106 } 107 } 108 109 return paramStr; 110 } 111 112 113 private HashMap<String, String> buildCompleteParams(String method, HashMap<String, String> parames) throws Exception{ 114 HashMap<String, String> commonParams = getCommonParams(method); 115 for (String key : parames.keySet()) { 116 if(commonParams.containsKey(key)){ 117 throw new Exception("参数名冲突"); 118 } 119 120 commonParams.put(key, parames.get(key)); 121 } 122 123 commonParams.put(KdtApiProtocol.SIGN_KEY, KdtApiProtocol.sign(appSecret, commonParams)); 124 return commonParams; 125 } 126 127 private HashMap<String, String> getCommonParams(String method){ 128 HashMap<String, String> parames = new HashMap<String, String>(); 129 parames.put(KdtApiProtocol.APP_ID_KEY, appId); 130 parames.put(KdtApiProtocol.METHOD_KEY, method); 131 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 132 sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); 133 parames.put(KdtApiProtocol.TIMESTAMP_KEY, sdf.format(new Date())); 134 parames.put(KdtApiProtocol.FORMAT_KEY, format); 135 parames.put(KdtApiProtocol.SIGN_METHOD_KEY, signMethod); 136 parames.put(KdtApiProtocol.VERSION_KEY, Version); 137 return parames; 138 } 139 }