Java 用post请求调用webservice

一般情况下,我们可以通过soapUi请求验证是否成功

对于webService的http请求,请求头必须添加SOAPAction
connection.setRequestProperty(“SOAPAction”,“application/soap+xml; charset=utf-8”);

xml转json需要用到hutool-all-4.0.12.jar,解析json比xml要简单得多。

如果不需要用xml转json,除需要jdk自带的包不再需要引入其他的包。

package com.baidu.util;
 
import cn.hutool.json.XML;
import net.sf.json.JSONObject;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
/** 
 * @version 1.0.0
 * @ClassName SoapUtil.java
 * @Description webservice调用工具类
 * @createTime 2020/9/29 10:37
 */
public class SoapUtil {
 
/**
     * 发送http post调用webservice
     *
     * @param strUrl
     * @param body
     * @return
     * @throws IOException
     */
    public static String soapPost(String strUrl, String body) throws IOException {
        PrintWriter printWriter = null;
        BufferedReader bufferedReader = null;
        StringBuilder responseResult = new StringBuilder();
        HttpURLConnection httpURLConnection = null;
        //第一步:创建服务地址,不是WSDL地址
        try {
            URL url = new URL(strUrl);
            //第二步:打开一个通向服务地址的连接
            httpURLConnection = (HttpURLConnection) url.openConnection();
            //第三步:设置参数
            // 设置通用的请求属性
            httpURLConnection.setConnectTimeout(20 * 1000);
            httpURLConnection.setReadTimeout(30 * 1000);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("content-type", "text/xml;charset=utf-8");
            //请求头必须设置SOAPAction
            httpURLConnection.setRequestProperty("SOAPAction", "application/soap+xml; charset=utf-8");
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            printWriter = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8"));
            printWriter.write(body);
            // flush输出流的缓冲
            printWriter.flush();
            //第五步:接收服务端响应,打印
            int responseCode = httpURLConnection.getResponseCode();
            StringBuilder sb = new StringBuilder();
            if (200 == responseCode) {//表示服务端响应成功
                // 定义BufferedReader输入流来读取URL的ResponseData
                bufferedReader = new BufferedReader(new InputStreamReader(
                    httpURLConnection.getInputStream(), Charset.forName("UTF-8")));
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    responseResult.append(line);
                }
            } else {
                throw new RuntimeException("调用webservice失败:服务器端返回HTTP code " + responseCode + "信息:");
            }
            return sb.toString();
        } catch (Exception e) {
             
        } finally {
            try {
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
                if (printWriter != null) {
                    printWriter.close();
                }
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException ex) {
            }
        }
    
    public static void main(String[] args)  {
    	String url = "http://localhost:9091/demo/validate";
        String body = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.webservice.caau.demo.com/\">\n"
            + "   <soapenv:Header/>\n"
            + "   <soapenv:Body>\n"
            + "      <ser:validate>\n"
            + "         <certBase64>123456</certBase64>\n"
            + "      </ser:validate>\n"
            + "   </soapenv:Body>\n"
            + "</soapenv:Envelope>";
         System.out.println(soapPost(url,body));
     }
 
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值