webservice 和 RESTful API 接口调用

Posted 七彩的人生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于webservice 和 RESTful API 接口调用的主要内容,如果未能解决你的问题,请参考以下文章

webservice和restful的区别

使用 Python 和 Flask 设计 RESTful API

如果我希望我的 iOS 应用程序使用返回 XML 数据的 RESTful WebService,我想开始学习哪些 API?

如何从JSP页面调用RestFul Webservice并使用JSON对象?

Rest,Rest Api,Web Service,RestFul Api之间的区别[关闭]

RESTful Web API 理解