如何将弃用的SoapService调用转换为新的UrlFetchApp [英] How to convert an deprecated SoapService call to the new UrlFetchApp
问题描述
我在这里找到了一个关于如何使用谷歌驱动器脚本调用web服务的示例: https://developers.google .com / apps-script / articles / soap_geoip_example
函数determineCountryFromIP(ipAddress){
var wsdl = SoapService.wsdl(http://www.webservicex.net/geoipservice.asmx?wsdl);
var geoService = wsdl.getGeoIPService();
var param = Xml.element(GetGeoIP,[
Xml.attribute(xmlns,http://www.webservicex.net/),
Xml.element(IPAddress,[
ipAddress
])
]);
var result = geoService.GetGeoIP(param);
返回result.Envelope.Body.GetGeoIPResponse.GetGeoIPResult.CountryCode.Text;
}
但是,这会使用不推荐使用的SoapService。该文档说我应该使用 UrlFetchApp
转换输入XML很容易。但是谁能告诉我如何使用UrlFetchApp调用webservice?
函数UrlFetchAppDetermineCountryFromIP_(ipAddress){
var xml =
<?xml version = \\ \\1.0 \编码= \\UTF-8 \\?>>
+< SOAP-ENV:Envelope xmlns:SOAP-ENV = \http://schemas.xmlsoap.org/soap/envelope/\xmlns:SOAP-ENC = \http: //schemas.xmlsoap.org/soap/encoding/\xmlns:xsd = \http://www.w3.org/2001/XMLSchema\xmlns:xsi = \http:// www .w3.org / 2001 / XMLSchema-instance \SOAP-ENV:encodingStyle = \http://schemas.xmlsoap.org/soap/encoding/\>
+< SOAP-ENV:Body>
+< GetGeoIP xmlns = \http://www.webservicex.net/\>
+< IPAddress>+ ipAddress +< / IPAddress>
+< / GetGeoIP>
+< / SOAP-ENV:Body>
+< / SOAP-ENV:Envelope>
var options =
{
method:post,
contentType:text / xml,
payload: xml
};
var result = UrlFetchApp.fetch(http://www.webservicex.net/geoipservice.asmx?wsdl,options);
var xmlResult = XmlService.parse(result).getRootElement();
var soapNamespace = xmlResult.getNamespace(soap);
var getGeoIPResponse = xmlResult.getChild(Body,soapNamespace).getChildren()[0];
var getGeoIPResponseNamespace = getGeoIPResponse.getNamespace();
$ b返回getGeoIPResponse
.getChild(GetGeoIPResult,getGeoIPResponseNamespace)
.getChild(CountryCode,getGeoIPResponseNamespace)
.getText();
$ b 使用 XmlService ,但是我尝试了几个小时,无法在Evnelope元素上放置4个xmlns属性,导致web服务请求失败
I found an example on how to call a webservice with google drive scripts here: https://developers.google.com/apps-script/articles/soap_geoip_example
function determineCountryFromIP(ipAddress) {
var wsdl = SoapService.wsdl("http://www.webservicex.net/geoipservice.asmx?wsdl");
var geoService = wsdl.getGeoIPService();
var param = Xml.element("GetGeoIP", [
Xml.attribute("xmlns", "http://www.webservicex.net/"),
Xml.element("IPAddress", [
ipAddress
])
]);
var result = geoService.GetGeoIP(param);
return result.Envelope.Body.GetGeoIPResponse.GetGeoIPResult.CountryCode.Text;
}
However this uses the SoapService wich is deprecated. the documentation says i should use UrlFetchApp
Converting the input xml is easy. But can anyone tell me how to call a webservice with the UrlFetchApp?
解决方案 It turns out to be a lot more work, but after a day of googling and trying i got it to work with the UrlFetchApp
function UrlFetchAppDetermineCountryFromIP_(ipAddress) {
var xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+"<SOAP-ENV:Body>"
+"<GetGeoIP xmlns=\"http://www.webservicex.net/\">"
+"<IPAddress>"+ ipAddress +"</IPAddress>"
+"</GetGeoIP>"
+"</SOAP-ENV:Body>"
+"</SOAP-ENV:Envelope>"
var options =
{
"method" : "post",
"contentType" : "text/xml",
"payload" : xml
};
var result = UrlFetchApp.fetch("http://www.webservicex.net/geoipservice.asmx?wsdl", options);
var xmlResult = XmlService.parse(result).getRootElement();
var soapNamespace = xmlResult.getNamespace("soap");
var getGeoIPResponse = xmlResult.getChild("Body", soapNamespace).getChildren()[0];
var getGeoIPResponseNamespace = getGeoIPResponse.getNamespace();
return getGeoIPResponse
.getChild("GetGeoIPResult", getGeoIPResponseNamespace)
.getChild("CountryCode", getGeoIPResponseNamespace)
.getText();
}
It should probely be posable to build the payload xml with the XmlService, however i tryed that for a few hours and was unable to put the 4 xmlns attributes on the Evnelope element, wich caused the webservice request to fail
这篇关于如何将弃用的SoapService调用转换为新的UrlFetchApp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!