webService

1.服务端远程服务
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.constants.Style;
import org.apache.axis.constants.Use;
import org.apache.axis.description.OperationDesc;
import org.apache.axis.description.ParameterDesc;

import javax.xml.namespace.QName;
import java.net.URL;
import java.util.Vector;

/**
 * @author yangyongzhen
 * @date 2019/11/8 21:23
 */
public class Demo {

    public static void main(String[] args) throws Exception {

        getWebServiceInfo("http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx");
    }

    public static String getWebServiceInfo(String url) throws Exception {

        OperationDesc oper = new OperationDesc();
        oper.setName("getStationName");
        ParameterDesc param;
        //创建service 实例
        Service service = new Service();
        //通过service 创建Call 实例
        Call call = (Call) service.createCall();
        //将Web Service的服务路径加入到call实例之中
        call.setTargetEndpointAddress(new URL(url));
        QName qname = new QName("http://WebXml.com.cn/", "getStationName");
        //设置调用远程方法的路径
        call.setOperationName(qname);
//        param = new ParameterDesc(new QName("http://WebXml.com.cn/","TrainCode"),ParameterDesc.IN,new QName("http://www.w3.org/2001/XMLSchema","String"),String.class,false,false);
//        param.setOmittable(true);
//        oper.addParameter(param);
//        param = new ParameterDesc(new QName("http://WebXml.com.cn/","UserId"),ParameterDesc.IN,new QName("http://www.w3.org/2001/XMLSchema","String"),String.class,false,false);
//        param.setOmittable(true);
//        oper.addParameter(param);
        //设置返回类型
        oper.setReturnType(new QName("http://xml.apache.org/xml-soap", "Vector"));
        oper.setStyle(Style.WRAPPED);
        oper.setUse(Use.LITERAL);
        call.setOperation(oper);
        call.setSOAPActionURI("http://WebXml.com.cn/getStationName");
        call.setUseSOAPAction(true);
//        call.setProperty(Call.SEND_TYPE_ATTR, Boolean.FALSE);
//        call.setProperty(AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
//        call.setSOAPVersion(SOAPConstants.SOAP12_CONSTANTS);
        //调用方法,返回一维数组
        Vector<String> Msg = (Vector<String>) call.invoke(new Object[]{});
        System.out.println(Msg);
        return null;
    }
}


2.客户端远程服务
/**
 * @author yangyongzhen
 * @date 2019/11/4 17:53
 */


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClientHandler {

    private static final String URL = "http://127.0.0.1:port/api";

    public static String sendPost(String param) throws Exception {
        try {
            // 创建httpClient实例对象
            HttpClient httpClient = new HttpClient();
            // 设置httpClient连接主机服务器超时时间:15000毫秒
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(150000);
            // 设置post请求超时时间
            httpClient.getHttpConnectionManager().getParams().setSoTimeout(150000);
            // 创建post请求方法实例对象
            PostMethod postMethod = new PostMethod(URL);
            postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
            postMethod.setRequestEntity(new StringRequestEntity(param, "text/xml", "UTF-8"));
            httpClient.executeMethod(postMethod);
            String result = null;
            if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
                result = postMethod.getResponseBodyAsString();
                System.out.println("responseResult:" + result);
            } else {
                System.out.println("method.getStatusCode()" + postMethod.getStatusCode());
            }
            postMethod.releaseConnection();
            return result;
        } catch (HttpException e) {
            e.printStackTrace();
            throw new HttpException(e.getMessage());
        }
    }
}    

你可能感兴趣的:(Java基础)