HttpClient 发送Soap get/post 请求

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Stephen_mu/article/details/87069960

Soap: Simple Object Access Protocol ——简单对象访问协议
        1.soap作为一个基于xml语言的协议用于网上传输数据
        2.SOAP = HTTP + XML
        3.SOAP是基于HTTP的 (这里顺便补充一下,Http是基于Socket的)
        4.SOAP的组成如下
               1.Envelope: 必须的,以XML根元素出现
               2.Header:可选的
               3.Body:必须的,在body部分包含一些,执行服务器的方法,和发送到服务器的数据    


/**
 * soap较rest具有比较好的可靠性与安全性
 * 
 * soap是一种固定的规范
 * 
 * 能够满足应用上下文信息与对话状态管理
 * 
 * @author xxx
 *
 */
public class HttpClientSoap {

    private final static Logger LOG = LoggerFactory.getLogger(HttpClientSoap.class);

    public static String Get(List<String> params) {

        String content = "params=" + params.get(0);
        String wsdl = "http://localhost:8080/Base/testGet?" + content;
        int timeout = 5000;

        // httpClient发送Soap请求
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(wsdl);
        // 设置连接的超时时间
        client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
        // 设置读取超时时间
        client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
        try {

            // 设置请求头部
            get.setRequestHeader("SOAPAction", "");

            // 执行请求返回响应码
            int responseCode = client.executeMethod(get);
            // 通过响应码,判断连接是否成功
            if (responseCode >= HttpsURLConnection.HTTP_OK && responseCode <= HttpsURLConnection.HTTP_BAD_REQUEST) {
                // 连接成功,获取响应内容
                InputStream is = get.getResponseBodyAsStream();
                String responseMsg = "";
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String line = "";
                if ((line = reader.readLine()) != null) {
                    responseMsg += line + "\n";
                }

                return responseMsg;

            }

        } catch (UnsupportedEncodingException e) {
            LOG.error("UnsupportedEncodingException", e);
        } catch (HttpException e) {
            LOG.error("HttpException", e);
        } catch (IOException e) {
            LOG.error("IOException", e);
        }
        return null;
    }

    public static String Post(List<String> params) {

        String wsdl = "http://localhost:8080/Base/testPost";
        int timeout = 5000;

        // 线程安全
        StringBuffer buffer = new StringBuffer("");

        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffer.append("<soap:Envelope " + "xmlns:api='http://demo.ls.com/' "
                + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
                + "xmlns:xsd='http://www.w3.org/2001/XMLSchema' "
                + "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
        buffer.append("<soap:Body>");
        buffer.append("<api:sayHello>");
        buffer.append("<arg0>ls</arg0>");
        buffer.append("</api:sayHello>");
        buffer.append("</soap:Body>");
        buffer.append("</soap:Envelope>");

        // httpClient发送Soap请求
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(wsdl);
        // 设置连接的超时时间
        client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
        // 设置读取超时时间
        client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
        try {
            // 将请求数据添加到soap请求体中
            RequestEntity entity = new StringRequestEntity(buffer.toString(), "text/xml", "UTF-8");
            // 设置请求头部
            post.setRequestHeader("SOAPAction", "");
            // 设置请求体
            post.setRequestEntity(entity);
            // 执行请求返回响应码
            int responseCode = client.executeMethod(post);
            // 通过响应码,判断连接是否成功
            if (responseCode >= HttpsURLConnection.HTTP_OK && responseCode <= HttpsURLConnection.HTTP_BAD_REQUEST) {
                // 连接成功,获取响应内容
                InputStream is = post.getResponseBodyAsStream();
                String responseMsg = "";
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String line = "";
                if ((line = reader.readLine()) != null) {
                    responseMsg += line + "\n";
                }
                return responseMsg;
            }

        } catch (UnsupportedEncodingException e) {
            LOG.error("UnsupportedEncodingException", e);
        } catch (HttpException e) {
            LOG.error("HttpException", e);
        } catch (IOException e) {
            LOG.error("IOException", e);
        }
        return null;
    }

}
 

猜你喜欢

转载自blog.csdn.net/Stephen_mu/article/details/87069960
今日推荐