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

java请求C# asmx接口[通俗易懂]packagecom.example.demo.controller;importorg.apache.axis.client.Call;importorg.apache.axis.client.Service;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind….

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

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>

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python excel转json json转excel[通俗易懂]

    python excel转json json转excel[通俗易懂]#-*-conding:utf-8-*-importopenpyxlfromopenpyxl.stylesimportPatternFill,Font,Alignment,Border,Sideimportopenpyxl.stylesasstyfromopenpyxlimportWorkbook,load_workbookimportjsonc…

    2022年5月1日
  • activiti与flowable的区别

    activiti与flowable的区别免费视频限时抢购:《Activiti6视频教程全家桶》《Flowable系列优惠套餐》《Flowable全家桶》《Camunda教程》《Drool7从入门到精通》在详细说明activiti与flowable的细节区别之前,我们需要说明一下这两个框架的发展史。我在写Activiti权威指南的时候,大概是2016年7月份左右。给清华大学出版社交稿的时候大概在2017年3月份…

    2022年5月11日
  • Python格式化字符串f-string概览

    Python格式化字符串f-string概览简介f-string,亦称为格式化字符串常量(formattedstringliterals),是Python3.6新引入的一种字符串格式化方法,该方法源于PEP498–LiteralStringInterpolation,主要目的是使格式化字符串的操作更加简便。f-string在形式上是以f或F修饰符引领的字符串(f’xxx’或F’xxx’),以大括号对{}标明…

    2022年6月10日
  • java 读写文件的两种方式

    java 读写文件的两种方式1.情景展示在实际开发过程中,对于文件的读写操作也是经常碰到的,如何用java完成对文件的准确无误的读写呢?2.场景分析在java中,我们可以通过数据流(二进制)来完成对文件的操作;其中,数据

    2022年7月1日
  • php httponly_php如何设置httponly

    php httponly_php如何设置httponlyphp设置httponly的方法:首先找到并打开“php.ini”文件;然后设置“session.cookie_httponly”项的值为1或者TRUE;接着通过“setrawcookie”方法开启即可。PHP设置Cookie的HTTPONLY属性httponly是微软对cookie做的扩展。这个主要是解决用户的cookie可能被盗用的问题。大家都知道,当我们去邮箱或者论坛登陆后,服务器会写一些c…

    2022年6月16日
  • 我的校园服务小程序_有创意校园的微信小程序

    我的校园服务小程序_有创意校园的微信小程序微信小程序——校园服务小程序(四)校园论坛加预约理发服务上一篇介绍了如何用户如何将帖子的内容发送到数据库中。这次我们来介绍一下如何将库中数据渲染出来,通过get得到对应表的数据,在wxml上通过for循环渲染数据表中的值。这里以我们的主页面为例,首先思考一下,一个展示帖子的主页面要有什么功能,1.帖子在添加时会将新的帖子放在最后,再渲染时也会被渲染在后面,这样是不可以的,每一次进入界面都是第一个用户上传的帖子。这里我们需要对帖子进行一次排序,这里我使用了orderBy(‘timeone’,‘d

    2022年9月20日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号