有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将JavaSOAP请求转换为Javascript?

我正在用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软件包来实现这一点,这也是一个公认的答案


共 (1) 个答案

  1. # 1 楼答案

    实际上,我雇了一个人来帮我解决这个问题!我们都不知道如何让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);
    });