Java对接WebService接口

一、使用WebClient方式实现调用;

1.WebService接口示例

POST /WebService.asmx HTTP/1.1
Host: tmscn-dev.carlsberg.asia
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetQrCode"


<soap:Envelope 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/">
  <soap:Header>
    <WSAuthentication xmlns="http://tempuri.org/">
      <UserName>stringUserName>
      <Password>stringPassword>
    WSAuthentication>
  soap:Header>
  <soap:Body>
    <GetQrCode xmlns="http://tempuri.org/">
      <BeginDate>stringBeginDate>
      <EndDate>stringEndDate>
    GetQrCode>
  soap:Body>
soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length


<soap:Envelope 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/">
  <soap:Body>
    <GetQrCodeResponse xmlns="http://tempuri.org/">
      <GetQrCodeResult>stringGetQrCodeResult>
    GetQrCodeResponse>
  soap:Body>
soap:Envelope>

2.代码如下:

package com.sigmatrix.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

@Component
public class WebServiceUtils {

    private static Logger logger = LoggerFactory.getLogger(WebServiceUtils.class);

    private static String qrCodeUrl = https://tmscn-dev.carlsberg.asia:8443/WebService.asmx

    /**
     * 请求头
     * @return
     */
    public static String getSoapHeader(String userName, String passWord){
        StringBuffer soapHeader = new StringBuffer();
        soapHeader.append("");
        soapHeader.append("");
        soapHeader.append(""+userName+"");
        soapHeader.append(""+passWord+"");
        soapHeader.append("");
        soapHeader.append("");
        return soapHeader.toString();
    }

    /**
     * 请求体
     * @return
     */
    public static String getAccInfoXml(String userName, String passWord, String beginDate, String endDate){
        StringBuffer template = new StringBuffer();
        String header = getSoapHeader(userName, passWord);
        template.append("");
        template.append(header);
        template.append("");
        template.append("");
        template.append(""+beginDate+"");
        template.append(""+endDate+"");
        template.append("");
        template.append("");
        template.append("");
        return template.toString();
    }

    /**
     * 访问WebService接口,获取数据
     * @return
     */
    public static String getAccInfo(String userName, String passWord, String beginDate, String endDate){
        String urlStr = qrCodeUrl;
        String paraXml = getAccInfoXml(userName, passWord, beginDate, endDate);
        String soapAction ="http://tempuri.org/GetQrCode";
        OutputStream out = null;
        String resultData = "";
        try {
            URL url = new URL(urlStr);
            HttpURLConnection con;
            con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestMethod("POST");
            con.setUseCaches(false);
            con.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
            con.setRequestProperty("SOAPAction", soapAction);
            out = con.getOutputStream();
            con.getOutputStream().write(paraXml.getBytes());
            out.flush();
            out.close();
            int code = con.getResponseCode();
            String tempString = null;
            StringBuffer sb1 = new StringBuffer();
            if (code == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
                while ((tempString = reader.readLine()) != null) {
                    sb1.append(tempString);
                }
                if (null != reader) {
                    reader.close();
                }
            } else {
                BufferedReader reader = new BufferedReader(new InputStreamReader(con.getErrorStream(), "UTF-8"));
                // 一次读入一行,直到读入null为文件结束
                while ((tempString = reader.readLine()) != null) {
                    sb1.append(tempString);
                }
                if (null != reader) {
                    reader.close();
                }
            }
            //响应报文
            resultData =  sb1.toString();
            System.out.println(resultData);

        } catch (Exception e) {
            logger.error("访问webService接口异常", e);
        }
        return resultData;
    }
}

二、根据wsdl文件来生成java-ws调用WebService接口

1.在cmd中执行以下命令,在指定目录中自动生成代码,调用webservice接口。

wsimport -XadditionalHeaders -s d:\webservice2 -p com.example.demo.request -encoding utf-8 https://tmscn-dev.carlsberg.asia:8443/WebService.asmx?WSDL

你可能感兴趣的:(java,webservice)