【发布时间】:2019-08-14 01:52:23
【问题描述】:
我正在使用 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变量中包含的数据获取到npm soap 包调用中。它似乎没有它的位置。 ???
非常感谢您提供任何想法/建议/信息!
更新:如果有人想展示如何使用替代的肥皂包来做到这一点,那也是一个可以接受的答案!
【问题讨论】:
-
我以前使用过 SOAP 服务,但我没有使用过这个特定的客户端。但是,浏览文档似乎需要一个 WSDL 文件,该文件是服务的合同而不是服务端点。 WSDL 包含有关消息格式和可用服务的信息,客户端可以使用这些信息为您生成 XML,从而公开method named after the service。如果您只需要发布和接收 XML,则可以使用 axios 之类的通用 http 客户端。
-
该死,我的意思是说该方法是以动作命名的。从 WSDL 生成客户端后,您应该可以调用
client.ManageConsultantListRequest(args) -
@patonw,你用什么肥皂客户端?
-
@patonw,您能否发布一个答案,说明您将如何在您使用的肥皂客户端中处理此问题?
标签: javascript java node.js soap