所用的jar
所用的工具类MD5S,HttpRequest 。
</dependency>
<dependency><groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0.1</version>
</dependency>
/**
*
*/
package solrpage;
import java.security.MessageDigest;
/**
* @author Administrator
*
*/
public abstract class MD5s
{
public final static String MD5(String pwd) {
//用于加密的字符
char md5String[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
try {
//使用平台的默认字符集将此 String 编码为 byte序列,并将结果存储到一个新的 byte数组中
byte[] btInput = pwd.getBytes();
//信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值。
MessageDigest mdInst = MessageDigest.getInstance("MD5");
//MessageDigest对象通过使用 update方法处理数据, 使用指定的byte数组更新摘要
mdInst.update(btInput);
// 摘要更新之后,通过调用digest()执行哈希计算,获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) { // i = 0
byte byte0 = md[i]; //95
str[k++] = md5String[byte0 >>> 4 & 0xf]; // 5
str[k++] = md5String[byte0 & 0xf]; // F
}
//返回经过加密后的字符串
return new String(str);
} catch (Exception e) {
return null;
}
}
}
package solrpage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* Created by Administrator on 2018/6/5.
*/
public class HttpRequest {
public static String addUrl(String head, String tail) {
if (head.endsWith("/")) {
if (tail.startsWith("/")) {
return head.substring(0, head.length() - 1) + tail;
} else {
return head + tail;
}
} else {
if (tail.startsWith("/")) {
return head + tail;
} else {
return head + "/" + tail;
}
}
}
public synchronized static String postData(String url, Map<String, String> params, String codePage) throws Exception {
final HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(25 * 1000);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(30 * 1000);
final PostMethod method = new PostMethod(url);
if (params != null) { method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, codePage);
method.setRequestBody(assembleRequestParams(params)); }
String result = "";
try {
httpClient.executeMethod(method);
result = new String(method.getResponseBody(), codePage);
}
catch (final Exception e) {
throw e;
} finally {
method.releaseConnection();
}
return result; }
/** * 缁勮http璇锋眰鍙傛暟 * * @param params * @param menthod * @return */
private synchronized static NameValuePair[] assembleRequestParams(Map<String, String> data) {
final List<NameValuePair> nameValueList = new ArrayList<NameValuePair>();
Iterator<Map.Entry<String, String>> it = data.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
nameValueList.add(new NameValuePair((String) entry.getKey(), (String) entry.getValue()));
}
return nameValueList.toArray(new NameValuePair[nameValueList.size()]);
}
}
//调用方法
public ExpressRes<CrtExpressVo> expressCheck(CrtExpressReq req) {
ExpressRes<CrtExpressVo> resp=new ExpressRes<CrtExpressVo>();
if(req.getCom()!=null&&!"".equals(req.getCom())){
String customer ="9D1BBF805ECC4F45848326D8696898A2";
String key = "tEsthbVfFLJp7133";
String param ="{\"com\":\""+req.getCom()+"\",\"num\":\""+req.getNum()+"\"}";
//String param ="{'com:'"+req.getCom()+"',num:'"+req.getNum()+"}";
String sign =MD5s.MD5(param+key+customer);
HashMap<String,String> params = new HashMap<>();
params.put("customer",customer);
params.put("sign",sign);
params.put("param",param);
try {
String res = new HttpRequest().postData("http://poll.kuaidi100.com/test/poll/query.do", params, "utf-8").toString();
CrtExpressVo vo=new CrtExpressVo();
vo.setResult(res);
resp.setVo(vo);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
return resp;
}
resp.setResMsg("输入正确的订单号或匹配的物流公司");
return resp;
}