zoukankan      html  css  js  c++  java
  • soap get/post请求

    pom.xml依赖:

    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
    </dependency>
      public static String getSoapRequest(String getUrl, String soapXml, String soapAction, String userName, String password) throws UnsupportedEncodingException {
        String retStr = "";
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // 设置BasicAuth
        CredentialsProvider provider = new BasicCredentialsProvider();
        // Create the authentication scope
        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
        // Create credential pair,在此处填写用户名和密码
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
        // Inject the credentials
        provider.setCredentials(scope, credentials);
        // Set the default credentials provider
        httpClientBuilder.setDefaultCredentialsProvider(provider);
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpGet httpGet = new HttpGet(getUrl);
        //  设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
        httpGet.setConfig(requestConfig);
        try {
    //      httpGet.setHeader("Content-Type", "text/xml;charset=UTF-8");
          StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
          httpGet.setHeader("SOAPAction", soapAction);
          CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
          HttpEntity httpEntity = response.getEntity();
          if (httpEntity != null) {
            // 打印响应内容
            retStr = EntityUtils.toString(httpEntity, "UTF-8");
            logger.info("response:" + retStr);
          }
          // 释放资源
          closeableHttpClient.close();
        } catch (Exception e) {
          logger.error("exception in doGetRequest", e);
        }
        return retStr;
      }
    

      

    post请求:

    static int socketTimeout = 30000;// 请求超时时间
    static int connectTimeout = 30000;// 传输超时时间
    static Logger logger = Logger.getLogger(HttpClientSoapUtil.class);

    /**
    * 使用SOAP1.1发送消息,可调1.1,也可调用1.2
    */
    public static String doPostSoap1_1WithBasicAuth(String postUrl, String soapXml, String soapAction, String userName, String password) {
    String retStr = "";
    // 创建HttpClientBuilder
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    // 设置BasicAuth
    CredentialsProvider provider = new BasicCredentialsProvider();
    // Create the authentication scope
    AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
    // Create credential pair,在此处填写用户名和密码
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
    // Inject the credentials
    provider.setCredentials(scope, credentials);
    // Set the default credentials provider
    httpClientBuilder.setDefaultCredentialsProvider(provider);
    // HttpClient
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

    HttpPost httpPost = new HttpPost(postUrl);
    // 设置请求和传输超时时间
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
    httpPost.setConfig(requestConfig);
    try {
    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("response:" + retStr);
    }
    // 释放资源
    closeableHttpClient.close();
    } catch (Exception e) {
    logger.error("exception in doPostSoap1_1", e);
    }
    return retStr;
    }
  • 相关阅读:
    JVM内存逃逸
    SQL中游标的使用
    配置JAVA环境变量中CLASSPATH变量的作用
    什么是单点登录?单点登录的三种实现方式
    oracle中 connect by prior 递归算法
    test
    mac idea 常见错误记录
    mac 常用操作命令记录
    mac idea 常用快捷键记录
    运行maven install命令时出现错误(BUILD FAILURE)
  • 原文地址:https://www.cnblogs.com/qq1141100952com/p/11114565.html
Copyright © 2011-2022 走看看