您的位置 首页 java

Java,WebService,SOAP-UI生成SOAP,HTTPClient调用服务

SOAP -UI

SoapUI 是为Windows、Mac和 Linux 操作系统开发的免费程序。作为测试SOAP( 简单对象访问协议 )和REST(代表性状态传输)web协议的一种方法,SoapUI提供了执行许多不同测试所需的工具和功能。这些测试包括功能测试、负载测试、安全测试和模拟。作为一个开源程序, soap UI是市场上第一个同类的API测试工具,并且在开发人员的帮助下已经形成了今天的产品。

地址:,

生成SOAP

SOAP请求报文:

 POST  HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml; charset =UTF-8
SOAPAction: ""
Content-Length: 299
Host: 192.168.3.50:9199
Connection: Keep-Alive
User-Agent: Apache- HTTPClient /4.5.5 ( java /17.0.2)

<soapenv:Envelope xmlns:soapenv="#34; xmlns:ser="#34;>
   <soapenv: Header />
   <soapenv:Body>
      <ser:userInfo>
         <!--Optional:-->
         <username>?</username>
      </ser:userInfo>
   </soapenv:Body>
</soapenv:Envelope>  

SOAP响应报文

 HTTP/1.1 200 
Content-Type: text/xml;charset=UTF-8
Content-Length: 208
Date: Fri, 11 Mar 2022 11:00:04 GMT
Keep-Alive: timeout=60
Connection: keep-alive

<soap:Envelope xmlns:soap="#34;>
    < soap:Body >
        <ns2:userInfoResponse xmlns:ns2="#34;>
            <String>?</String>
        </ns2:userInfoResponse>
    </soap:Body>
</soap:Envelope>  

HttpClient 调用WebService服务

pom.xml

 <!-- HttpClient-->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.13</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpasyncclient</artifactId>
    <version>4.1</version>
</dependency>  

客户端调用

 package com.what21.demo.service;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config. request Config;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import java.io.IO Exception ;

public class WebServiceClientDemo {

    /**
     * @param args
     * @return
     */    public  static   void  main(String[] args) {
        String url = "#34;;
        String xml = "<soapenv:Envelope xmlns:soapenv="#34; xmlns:ser="#34;>n" +
                "   <soapenv:Header/>n" +
                "   <soapenv:Body>n" +
                "      <ser:userInfo>n" +
                "         <!--Optional:-->n" +
                "         <username>?</username>n" +
                "      </ser:userInfo>n" +
                "   </soapenv:Body>n" +
                "</soapenv:Envelope>";
         String  result = null;
        HttpEntity httpEntity = null;
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            // 创建POST
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(getRequestConfig());
            // 设置头参数
            Header  ContentType Header = new BasicHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader(contentTypeHeader);
            Header soapActionHeader = new BasicHeader("SOAPAction", "");
            httpPost.setHeader(soapActionHeader);
            // 头设置报文和通讯格式
            StringEntity stringEntity = new StringEntity(xml, ContentType.TEXT_XML);
            stringEntity.setContentEncoding("UTF-8");
            httpPost.setEntity(stringEntity);
            // 执行
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(httpEntity);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                EntityUtils.consume(httpEntity);
            } catch (IOException e) {
                // http请求释放资源异常
                e.printStackTrace();
            }
        }
        System.out.println(result);
    }

    /**
     * @return
     */    public static RequestConfig getRequestConfig() {
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(180 * 1000) // 连接超时时间
                .setConnectTimeout(180 * 1000) // 请求超时时间
                .setConnectionRequestTimeout(180 * 1000) // 连接请求超时时间
                .setRedirectsEnabled(true) // 重定向开关
                .build();
        return requestConfig;
    }

}  

文章来源:智云一二三科技

文章标题:Java,WebService,SOAP-UI生成SOAP,HTTPClient调用服务

文章地址:https://www.zhihuclub.com/197980.shtml

关于作者: 智云科技

热门文章

网站地图