javascript - 将 Java SOAP 请求翻译成 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 包。
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 包调用。它似乎没有它的位置。???
非常感谢您的任何想法/建议/信息!
更新:如果有人想展示如何使用替代的肥皂包来做到这一点,那也是一个可以接受的答案!
解决方案
实际上,我在 UpWork 上花钱请人帮我解决这个问题!我们俩都不知道如何让 npmsoap
包在这个 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);
});
推荐阅读
- android - 搜索 ListView 不起作用
- vba - Tranfser data between two closed workbooks without creating a new one
- angular - Detect keyboard navigation in angular material mat-selection-list
- python - Python :- func() 有多个关键字值?
- php - 在 ubuntu 中运行 laravel 应用程序时出错
- pandas - Parse JSON to Excel - Pandas + xlwt
- php - Php remove node from xml containing string
- javascript - Prevent beforeunload function from executing if submit button is clicked
- wordpress - Wordpress、save_post、post_status 并不是数据库中的内容
- google-apps-script - 在 Apps 脚本中获取日历事件日期时出现类型错误:“尝试“getStartTime()”时出现类型错误”