webservice 和 RESTful API 接口调用

一、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

 

 编码问题,接收方收到的乱码

StringEntity se = new StringEntity(new String(jsonstr.getBytes("utf-8"),"utf-8"));

重点是对StringEntity 进行编码

SSL类

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;


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

 

注意pom

报错异常:org.apache.http.annotation.ThreadSafe的类文件

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.2</version>
</dependency>

 

posted @ 2018-02-05 10:27  lyon♪♫  阅读(1033)  评论(0编辑  收藏  举报