javascript - 将 Java SOAP 请求转换为 Javascript?

标签 javascript java node.js soap

我正在努力用 Javascript 发出 SOAP 请求。我以前从未做过 SOAP 请求,而且服务提供者只有 Java 的示例代码。

这是他们的 Java 示例代码:

    String applicationPath = "c:\\e-Notify\\";
    String inputDirectory = "inputs\\";
    String outputDirectory = "outputs\\";
    String url = "https://example.com/ENotifyService.svc";  
    String xml = "";
    String resp = "";
    String action = "";
    String inputFilePath = "";
    String outputFilePath = "";     
    try {
        //Encode the URL
        URL urlx = new URL(url);
        //Instance of connection object
        HTTPRequestPoster poster = new HTTPRequestPoster();         
        //Character stream
        Reader data = new StringReader("");

        //Get the XML from the input file
        inputFilePath = applicationPath + inputDirectory + "manage-consultant-list-input.xml";
        xml = FileReader(inputFilePath);
        data = new StringReader(xml);
        //Set operation
        action = "ManageConsultantListRequest";
        //Send request to server and get the response.
        poster = new HTTPRequestPoster();
        resp = poster.postData(data, urlx, action); <==NOTE `ACTION` VARIABLE
        //Write the response to the output file
        outputFilePath = applicationPath + outputDirectory + "manage-consultant-list-output.xml";
        FileWriter(outputFilePath, resp);           
    }
    catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

根据 SOAP API 所有者提供的示例代码,我需要发送以下值:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <ManageConsultantListRequest xmlns="http://com.example.services.ServiceModel/2012/eNotifyService">
      <Credentials xmlns:a="http://com.example.services.ServiceModel/2012/Credentials" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Username>MyAPIUsername</a:Username>
        <a:Password>MyAPIPassword#1</a:Password>
      </Credentials>
      <Consultants xmlns:a="http://com.example.services.ServiceModel/2012/eNotify" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Consultant>
          <a:SubmissionActionCode>A</a:SubmissionActionCode>
          <a:Jurisdiction>IL</a:Jurisdiction>
          <a:LicenseNumber>00000001</a:LicenseNumber>
        </a:Consultant>
      </Consultants>
      <TransactionID>12345</TransactionID>
    </ManageConsultantListRequest>
  </s:Body>
</s:Envelope>

我正在查看npm Soap package

import soap from 'soap'; //https://www.npmjs.com/package/soap

let url = 'https://example.com/ENotifyService.svc';
let args = {name: 'value'};
soap.createClient(url, function(err, client) {
    client.MyFunction(args, function(err, result) { <==WHERE TO PUT DATA FROM `ACTION` VARIABLE??
        console.log(result);
    });
});

我认为我可能可以使用 https://davidwalsh.name/convert-xml-json 中描述的技术将我需要的 XML 数据转换为 JSON 格式。 .

我还没弄清楚:

  • 如何将 Java action 变量中包含的数据获取到 npmsoap 包调用中。似乎没有地方放它。 ???

提前非常感谢您的任何想法/建议/信息!

更新:如果有人想展示如何使用替代 SOAP 包来做到这一点,这也将是一个可接受的答案!

最佳答案

实际上,我在 UpWork 上付钱某人来帮助我解决这个问题!我们俩都不知道如何让 npm soap 包在这个 API 上工作。然而,他提供了这种替代方法,该方法确实有效:

var request = require("request");
var rawXML = ''; //your xml goes here
var options = { method: 'POST',
    url: 'https://example.com/eNotifyService.svc',
    qs: { asmx: '' },
    headers:
        { 'cache-control': 'no-cache',
            Connection: 'keep-alive',
            'content-length': '1960',
            'Cache-Control': 'no-cache',
            Accept: '*/*',
            'User-Agent': 'PostmanRuntime/7.15.0',
            Host: 'example.com',
            SOAPAction: '"requestedAction"',
            'Accept-Encoding': 'gzip,deflate',
            'Content-Type': 'text/xml;charset=UTF-8' },
    body: rawXML };

request(options, function (error, response, body) {
    if (error) throw new Error(error);

    console.log(body);
});

关于javascript - 将 Java SOAP 请求转换为 Javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57487241/

相关文章:

javascript - 奇怪的行为 IE8 选择框向上显示并超出浏览器限制

javascript - 动态添加@font-face到svg

javascript - 在 .focusout() 之前触发表单提交

java - Android InApp Purchase - 如何处理待定状态?

java - PhpStorm 未知 key 代码 : 0x0

java - 计算多个字符串之间的编辑距离

Node.js "FATAL ERROR: JS Allocation failed - process out of memory"- 可以获得堆栈跟踪吗?

javascript - 错误: No default engine was specified and no extension was provided.简单路由

javascript - 使用 Bootstrap Accordion 时,单选按钮无法与数据切换一起使用

javascript - react-waypoint onEnter/onLeave 没有给出值