java請求C# asmx接口

package com.example.demo.controller;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;

import javax.xml.namespace.QName;
import java.io.*;
import java.rmi.ConnectException;
import java.util.Random;
import java.util.UUID;

/**
 * Created with IDEA
 * author:QinWei
 * Date:2019/12/2   0002
 * Time:16:57
 */
@Controller
public class indexController {

    @RequestMapping("/index")
    @ResponseBody
    public Object index(){
        return WebService();
    }

    public  static  String WebService(){

        File file = new File("C:\\pdf.pdf");
        String str = PDFToBase64(file);
        // webService鏈接地址 正式環境請更換至配置文件中以防修改方便
        String url = "http://IP/XXX.asmx";
        String uuid = genUniqueKey();
        // server域名地址,爲了統一規範,一般都是這個域名 固定地址不需要配置
        String soapaction = "http://tempuri.org/";
        // 方法名
        String methodName = "DocumentPush";
        // 用戶提供測試的兩個參數
        String action  = "Create";
        String ref = null;
        
        StringBuffer sb = new StringBuffer();
      //XML拼接
        sb.append("<REQUEST>");
        sb.append("<RESPONSEITEM>");
        sb.append("<PATIENTID>T001688785</PATIENTID>");
        sb.append("</RESPONSEITEM>");
        sb.append("</REQUEST>");
        System.out.println("=========打印============"+sb);
        Service service = new Service();
        try{
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            // 設置要調用哪個方法
            call.setOperationName(new QName(soapaction,methodName));
            // 設置要傳遞的參數名
            call.addParameter(new QName(soapaction,"action"),org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.addParameter(new QName(soapaction,"message"),org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            // 提供標準類型
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapaction + methodName);
            // 調用方法並傳遞參數
             ref = (String)call.invoke(new Object[]{action,sb.toString()});
            System.out.println("=========打印============"+ref);
            return ref;
        }catch (ConnectException ex){
            ex.printStackTrace();
            System.err.println("=========連接超時=========");
        } catch (Exception e){
            e.printStackTrace();
        }
        ref = "鏈接失敗";
        return ref;
    }

    public static void main(String []ages){
        //WebService();

        //System.out.println(str);
    }


    /*
    生成唯一主鍵
    格式:時間+隨機數
     */
    public static synchronized String genUniqueKey() {
        Random random = new Random();
        Integer number = random.nextInt(900000) + 100000;
        return System.currentTimeMillis() + String.valueOf(number);
    }


    /**
     * Description: 將pdf文件轉換爲Base64編碼
     * @param  file
     * @Author qinwei
     * Create Date: 2015年8月3日 下午9:52:30
     */
    public static String PDFToBase64(File file) {

        BASE64Encoder encoder = new BASE64Encoder();
        FileInputStream fin =null;
        BufferedInputStream bin =null;
        ByteArrayOutputStream baos = null;
        BufferedOutputStream bout =null;
        try {
            fin = new FileInputStream(file);
            bin = new BufferedInputStream(fin);
            baos = new ByteArrayOutputStream();
            bout = new BufferedOutputStream(baos);
            byte[] buffer = new byte[1024];
            int len = bin.read(buffer);
            while(len != -1){
                bout.write(buffer, 0, len);
                len = bin.read(buffer);
            }
            //刷新此輸出流並強制寫出所有緩衝的輸出字節
            bout.flush();
            byte[] bytes = baos.toByteArray();
            return encoder.encodeBuffer(bytes).trim();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fin.close();
                bin.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


}

以下爲需要的maven依賴

<!-- 以下均爲webservice調用的maven依賴 -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.5</version>
        </dependency>

        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章