package com.zl.httpClient4;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.NameValuePair;
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.BasicHeader;
import org.apache.http.util.EntityUtils;
public class Demo1 {
public static void main(String[] args)throws Exception {
String url = "http://127.0.0.1:8080/cuschk/api/inspection/ledShow.action";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
httpPost.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"));
httpPost.setHeader("source", "yingdi");
httpPost.setHeader("sign", getSign());
// List <NameValuePair> params = new ArrayList<NameValuePair>();
// params.add(new BasicNameValuePair("sup_zone", "上海"));
// params.add(new BasicNameValuePair("chk_loc", "武汉"));
// httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
CloseableHttpClient httpClient=HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
System.out.println(result);
httpResponse.close(); // response关闭
httpClient.close(); // httpClient关闭
}
public static String getSign(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
String date = df.format(new Date());// new Date()为获取当前系统时间
String encryptStr="yingdi"+date;
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
byte[] md5Bytes = md5.digest(encryptStr.getBytes());
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
encryptStr = hexValue.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
return encryptStr.substring(8, 24).toUpperCase();
}
}
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.NameValuePair;
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.BasicHeader;
import org.apache.http.util.EntityUtils;
public class Demo1 {
public static void main(String[] args)throws Exception {
String url = "http://127.0.0.1:8080/cuschk/api/inspection/ledShow.action";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
httpPost.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"));
httpPost.setHeader("source", "yingdi");
httpPost.setHeader("sign", getSign());
// List <NameValuePair> params = new ArrayList<NameValuePair>();
// params.add(new BasicNameValuePair("sup_zone", "上海"));
// params.add(new BasicNameValuePair("chk_loc", "武汉"));
// httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
CloseableHttpClient httpClient=HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
System.out.println(result);
httpResponse.close(); // response关闭
httpClient.close(); // httpClient关闭
}
public static String getSign(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
String date = df.format(new Date());// new Date()为获取当前系统时间
String encryptStr="yingdi"+date;
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
byte[] md5Bytes = md5.digest(encryptStr.getBytes());
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
encryptStr = hexValue.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
return encryptStr.substring(8, 24).toUpperCase();
}
}