前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java请求C# asmx接口[通俗易懂]

java请求C# asmx接口[通俗易懂]

作者头像
全栈程序员站长
发布2022-08-26 21:44:19
7510
发布2022-08-26 21:44:19
举报

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
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依赖

代码语言:javascript
复制
<!-- 以下均为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>

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/145055.html原文链接:https://javaforall.cn

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022年5月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com