一、webservice
通过cxf 客户端根据接口地址生成代码,进行调用
针对https JaxWsProxyFactoryBean jax = new JaxWsProxyFactoryBean(); jax.setAddress(addr); jax.setServiceClass(IArvatoWS.class); IArvatoWS client = (IArvatoWS) jax.create(); // 自定义证书 HTTPConduit httpConduit = (HTTPConduit) ClientProxy.getClient(client).getConduit(); TLSClientParameters tlsCP = new TLSClientParameters(); TrustManager[] trusty = new javax.net.ssl.TrustManager[] { new MyX509TrustManager() }; tlsCP.setTrustManagers(trusty); tlsCP.setDisableCNCheck(true); httpConduit.setTlsClientParameters(tlsCP); client. 方法(); import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; public class MyX509TrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { // TODO Auto-generated method stub } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { // TODO Auto-generated method stub } public X509Certificate[] getAcceptedIssuers() { // TODO Auto-generated method stub return null; } }
二、RESTful
通过httpclient 调用
POST
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; 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; import com.bizdata.SSLClient; public class HttpUtils { @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"); httpPost.addHeader("source", "DT"); httpPost.addHeader("pwd", "111111"); httpPost.addHeader("marketcode", "067"); httpPost.addHeader("languagecode", "CN"); StringEntity se = new StringEntity(new String(jsonstr.getBytes("utf-8"))); se.setContentType("text/json"); se.setContentEncoding(new BasicHeader("Content-Type", "application/json")); httpPost.setEntity(se); 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; } } String result = HttpUtils.doPost(url, jsonStr, "utf-8");
GET
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.util.EntityUtils; public class TestGet { public static void main(String[] args) { String url = "http://139.219.199.233:9083/admin/proqrcode/list"; url = url + "?page=2&rows=5"; HttpClient httpClient = null; HttpGet httpGet = null; String result = null; try { httpClient = new SSLClient(); httpGet = new HttpGet(url); httpGet.addHeader("Content-Type", "application/json"); HttpResponse response = httpClient.execute(httpGet); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, "UTF-8"); } } } catch (Exception ex) { ex.printStackTrace(); } System.out.println("结果 " + result); } }