private void checkThread() {
Urls urls = new Urls(type);//根據惟一識別類型初始化參數,可根據實際狀況修改此構造函數
//訪問國家平臺接口,取出模板數據
JSONObject jo = new JSONObject();
jo.put("wstdid","ff80808167179c450167c015a1a30188");
urls.setData(jo.toString());
// String data = Self3des.self3desJiam(urls);//數據加密,根據實際加密方式改寫加密方法
String data = "testAccount%25%25%25%256%2BmbF25%2F62mHJz%2FYH6y7Woh4VROXmIf8uT4Kg7qPY66SlKpx1D%2F8nP32lbFMh6ZU%25%25%25%250fccdb9d8b2ef8dfa9e86312ad6967fd1139b6cbc99da1c2d55ba302fbc8cfde%25%25%25%251550542486952%25%25%25%25report_down_tpl";
//urls.setUrl(urls.getUrl()+"?datainfo="+data);
/* jo.put("datainfo", data);
String jostr = jo.toString();*/
String gjRlt = HttpClientUtil.doPost(urls.getUrl(), data, "utf-8");
JSONObject rlt = JSONObject.parseObject(gjRlt);
//對反饋結果進行判斷處理,取出數據錄入供水系統
if("0".equals(rlt.get("code"))){
urls.setData(rlt.getString("data"));
data = Self3des.self3desJiam(urls);//數據解密,根據實際解密方式改寫解密方法
//將取出的數據導入供水系統
String cxgsRlt = HttpClientUtil.doPost(urls.getcxgsurl(), data, "utf-8");
urls.setData(cxgsRlt);
}
}java
package cxgshttp.utils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
/**
* 利用HttpClient進行post請求的工具類
* @ClassName: HttpClientUtil
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:43:38
*
*/
public class HttpClientUtil {
@SuppressWarnings("resource")
public static String doPost(String url,String jsonstr,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
/* StringEntity se = new StringEntity(jsonstr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se);*/
//httpPost.setEntity(new StringEntity(jsonstr, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}apache
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}json
package cxgshttp.utils;app
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;ide
public class DESCryptography {
public static byte[] DES_CBC_Encrypt(byte[] content, byte[] keyBytes){
try {
DESKeySpec keySpec=new DESKeySpec(keyBytes);
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
SecretKey key=keyFactory.generateSecret(keySpec);
Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(keySpec.getKey()));
byte[] result=cipher.doFinal(content);
return result;
} catch (Exception e) {
System.out.println("exception:"+e.toString());
}
return null;
}
public static byte[] DES_CBC_Decrypt(byte[] content, byte[] keyBytes){
try {
DESKeySpec keySpec=new DESKeySpec(keyBytes);
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
SecretKey key=keyFactory.generateSecret(keySpec);
Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(keyBytes));
byte[] result=cipher.doFinal(content);
return result;
} catch (Exception e) {
System.out.println("exception:"+e.toString());
}
return null;
} 函數
}工具