zoukankan      html  css  js  c++  java
  • Java调用JavaWebService

    1.pom配置

            
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.5</version>
            </dependency>
            
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>fluent-hc</artifactId>
                <version>4.5.5</version>
            </dependency>
            
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                <version>4.4.9</version>
            </dependency>

    2.http请求代码

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import java.nio.charset.Charset;
    
    /**
     * HttpClient-invoke-WebService.
     * 
     * @author liwenxue
     */
    public class HttpUtil {
    
        static int SOCKET_TIME_OUT = 30000;
    
        static int CONNECT_TIME_OUT = 30000;
    
        static final Log LOGGER = LogFactory.getLog(HttpUtil.class);
    
        public static String doPostSoap(String postUrl, String soapXml,String soapAction) {
            String retStr = "";
            try 
            {
                HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        
                CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
                HttpPost httpPost = new HttpPost(postUrl);
        
                RequestConfig requestConfig = RequestConfig.custom()
                        .setSocketTimeout(SOCKET_TIME_OUT)
                        .setConnectTimeout(CONNECT_TIME_OUT).build();
                httpPost.setConfig(requestConfig);
                httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
                httpPost.setHeader("SOAPAction", soapAction);
                
                StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
                httpPost.setEntity(data);
                CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
                HttpEntity httpEntity = response.getEntity();
                
                if (httpEntity != null) {
                    retStr = EntityUtils.toString(httpEntity, "UTF-8");
                    LOGGER.info("登录请求正常返回.");
                }
                else{
                    LOGGER.info("登录请求未正常返回.");
                }
                closeableHttpClient.close();
            } catch (Exception e) {
                LOGGER.error("登录发生异常", e);
            }
            return retStr;
        }
    
    }

    3.调用示例

    String authUrl="WebService地址";
    String xml="WebService请求报文";
    String itName="接口名";
    String result = HttpUtil.doPostSoap(authUrl, xml,itName);
  • 相关阅读:
    测试结束的标准
    坚持“5W”规则,明确内容与过程
    单元测试过程各阶段的输入、输出是什么?
    集成测试过程各阶段的输入、输出是什么?
    系统测试过程各阶段的输入、输出是什么?
    Mybatis的优点和缺点?
    Hibernate中session有几种创建方式?都有那些区别?
    AQS结构
    JSP和Servlet有哪些相同点和不同点,他们之间的联系是什么?
    Mybatis 动态 sql 是做什么的?都有哪些动态 sql?能简述一下动态 sql 的执行原理不?
  • 原文地址:https://www.cnblogs.com/oumi/p/9140626.html
Copyright © 2011-2022 走看看