1. 程式人生 > >webservice 和 RESTful API 接口調用

webservice 和 RESTful API 接口調用

row ktr ger stub exe out src -s lap

一、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; } }
View Code

二、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");
View Code

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);
    }
}
View Code

webservice 和 RESTful API 接口調用