Webservice 接口调用

  • 最近项目中频繁遇到web service接口调用,调用起来麻烦,若使用wsimport生成客户端的方式调用,造成项目代码臃肿,简单整理个ws调用工具,便于在项目开发中使用。

  • WebServiceUtil
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

/**
 * Web service调用工具
 * @author Cheng.Wei
 * @date 2018年8月7日
 * @since jdk1.8
 */
public class WebServiceUtil {
    /**
     * 发送请求
     * @param request
     * @return
     * @throws IOException 
     */
    public static InputStream post(final Request request) throws IOException{
        URL url = new URL(request.getUrl());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.connect();
        if(request.getAccount()!=null){
            String auth = request.getAccount().getUsername() +":"+ request.getAccount().getPassword();
            byte[] authstr = Base64.getEncoder().encode(auth.getBytes());
            connection.setRequestProperty("Authorization", "Basic "+new String(authstr));
        }
        connection.connect();
        String soapXML = request.getBody();
        try(OutputStream os = connection.getOutputStream()){
            os.write(soapXML.getBytes());
            if(connection.getResponseCode()!=200){
                throw new IOException("response:"+connection.getResponseCode());
            }
            InputStream is = connection.getInputStream();
            return is;
        }
    }
}

  • Request 请求报文
/**
 * 请求报文
 * @author Cheng.Wei
 * @date 2018年8月7日
 */
public class Request {
    /**访问地址*/
    private String url;
    /**XML格式数据*/
    private String body;
    /**账号信息*/
    private Account account;


    public String getUrl() {
        return url;
    }


    public void setUrl(String url) {
        this.url = url;
    }


    public String getBody() {
        return body;
    }


    public void setBody(String body) {
        this.body = body;
    }


    public Account getAccount() {
        return account;
    }


    public void setAccount(Account account) {
        this.account = account;
    }

    @Override
    public String toString() {
        return "Request [url=" + url + ", body=" + body + ", account=" + account + "]";
    }

    /**
     * 账号信息
     * @author Cheng.Wei
     * @date 2018年8月7日
     */
    public class Account{
        /**账号*/
        private String username;
        /**密码*/
        private String password;
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "Account [username=" + username + ", password=" + password + "]";
        }
    }

}

  • *测试代码
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;

/**
 * Web service 测试
 * @author Cheng.Wei
 *
 * @date 2018年8月7日
 */
public class WsTest {
    /**
     * QQ在线
     * @throws IOException
     */
    @Test
    public void qqOnline() throws IOException {
        /**QQ号*/
        String qq = "10086";
        String requestXml =  ""
                            + ""
                            +    ""
                            +        ""
                            +           ""+qq+""
                            +        ""
                            +    ""
                            + "";
        Request request = new Request();
        request.setBody(requestXml);
        request.setUrl("http://ws.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl");
        try(InputStream inputStream = WebServiceUtil.post(request)){
            StringBuffer sb = new StringBuffer();
            byte[] b = new byte[1024];
            int len = 0;
            String str = null;
            while((len=inputStream.read(b)) != -1){
                str = new String(b, 0, len, "utf-8");
                sb.append(str);
            }
            System.out.println(sb.toString());
        }
    }
    /**
     * IP归属
     * @throws IOException 
     */
    @Test
    public void ipAddress() throws IOException{
        /**IP*/
        String ipAddress = "8.8.8.8";
        String requestXml =""
                            + ""
                            +   ""
                            +       ""
                            +           " "+ipAddress+""
                            +       ""
                            +   ""
                            +"";
        Request request = new Request();
        request.setBody(requestXml);
        request.setUrl("http://ws.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl");
        try(InputStream inputStream = WebServiceUtil.post(request)){
            StringBuffer sb = new StringBuffer();
            byte[] b = new byte[1024];
            int len = 0;
            String str = null;
            while((len=inputStream.read(b)) != -1){
                str = new String(b, 0, len, "utf-8");
                sb.append(str);
            }
            System.out.println(sb.toString());
        }
    }
}

  • 运行结果
    Webservice 接口调用_第1张图片

你可能感兴趣的:(工作记录)