SOAP API 从服务器返回 HTML 而不是 XML
Posted
技术标签:
【中文标题】SOAP API 从服务器返回 HTML 而不是 XML【英文标题】:SOAP API returning HTML from server instead of XML 【发布时间】:2018-10-02 15:15:03 【问题描述】:我可能不是唯一一个在 *** 上发布此问题的人,但不幸的是,他们都没有解决我的问题。
我开发了一个通过 SOAP API 连接到服务器的开放式移动应用程序,当通过 Postman 测试 API 时,它返回正确的结果,但是在运行应用程序时,API 返回 html 而不是 XML。它没有执行 php 文件并触发函数。
-(void)loginRequest
@try
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
NSString *password = [NSString stringWithFormat:@"%@", [self md5:SOAP_PASSWORD]];
[params setObject:SOAP_USER_NAME forKey:@"apiKey"];
[params setObject:password forKey:@"apiPassword"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString;
if(!jsonData)
// NSLog(@"Got an error: %@", error);
else
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *parameters = [NSString stringWithFormat:@"<attributes xsi:type=\"xsd:string\">%@</attributes>", jsonString];
NSString *envelope = [self createEnvelope:@"apiLogin" forNamespace:@"urn:Opencart" forParameters:parameters];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: URL]];
[request setHTTPMethod:@"POST"];
NSMutableDictionary *defaultHeaders = [[NSMutableDictionary alloc] init];
[defaultHeaders setValue:@"gzip" forKey:@"Accept-Encoding"];
[defaultHeaders setValue:@"en, en-us;q=0.8" forKey:@"Accept-Language"];
[defaultHeaders setValue:@"text/xml; charset=utf-8" forKey:@"Content-Type"];
[defaultHeaders setValue:@"urn:Opencart_Api_Model_Server_HandlerAction" forKey:@"SOAPAction"];
[defaultHeaders setValue:@"com.lognllc.Magento-ios-Example/1.0.0 (unknown, iPhone OS 8.1, iPhone Simulator, Scale/2.000000)" forKey:@"User-Agent"];
[request setAllHTTPHeaderFields:defaultHeaders];
[request setHTTPBody:[envelope dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(connection)
webData = [NSMutableData data];
else
NSLog(@"The Connection is null error: %@", error);
@catch (NSException *e)
NSLog(@"catching %@ reason 1 %@", [e name], [e reason]);
我从客户端服务器得到的返回结果如下:
<html><head><title>NuSOAP: Opencart</title>
<style type="text/css">
body font-family: arial; color: #000000; background-color: #ffffff; margin: 0px 0px 0px 0px;
p font-family: arial; color: #000000; margin-top: 0px; margin-bottom: 12px;
pre background-color: silver; padding: 5px; font-family: Courier New; font-size: x-small; color: #000000;
ul margin-top: 10px; margin-left: 20px;
li list-style-type: none; margin-top: 10px; color: #000000;
.content
margin-left: 0px; padding-bottom: 2em;
.nav
padding-top: 10px; padding-bottom: 10px; padding-left: 15px; font-size: .70em;
margin-top: 10px; margin-left: 0px; color: #000000;
background-color: #ccccff; width: 20%; margin-left: 20px; margin-top: 20px;
.title
font-family: arial; font-size: 26px; color: #ffffff;
background-color: #999999; width: 100%;
margin-left: 0px; margin-right: 0px;
padding-top: 10px; padding-bottom: 10px;
.hidden
position: absolute; visibility: hidden; z-index: 200; left: 250px; top: 100px;
font-family: arial; overflow: hidden; width: 600;
padding: 20px; font-size: 10px; background-color: #999999;
layer-background-color:#FFFFFF;
a,a:active color: charcoal; font-weight: bold;
a:visited color: #666666; font-weight: bold;
a:hover color: cc3300; font-weight: bold;
</style>
<script language="javascript" type="text/javascript">
<!--
// POP-UP CAPTIONS...
function lib_bwcheck() //Browsercheck (needed)
this.ver=navigator.appVersion
this.agent=navigator.userAgent
this.dom=document.getElementById?1:0
this.opera5=this.agent.indexOf("Opera 5")>-1
this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
this.ie=this.ie4||this.ie5||this.ie6
this.mac=this.agent.indexOf("Mac")>-1
this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
this.ns4=(document.layers && !this.dom)?1:0;
this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
return this
var bw = new lib_bwcheck()
//Makes crossbrowser object.
function makeObj(obj)
this.evnt=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document.layers[obj]:0;
if(!this.evnt) return false
this.css=bw.dom||bw.ie4?this.evnt.style:bw.ns4?this.evnt:0;
this.wref=bw.dom||bw.ie4?this.evnt:bw.ns4?this.css.document:0;
this.writeIt=b_writeIt;
return this
// A unit of measure that will be added when setting the position of a layer.
//var px = bw.ns4||window.opera?"":"px";
function b_writeIt(text)
if (bw.ns4)this.wref.write(text);this.wref.close()
else this.wref.innerHTML = text
//Shows the messages
var oDesc;
function popup(divid)
if(oDesc = new makeObj(divid))
oDesc.css.visibility = "visible"
function popout() // Hides message
if(oDesc) oDesc.css.visibility = "hidden"
//-->
</script>
</head>
<body>
<div class=content>
<br><br>
<div class=title>Opencart</div>
<div class=nav>
<p>View the <a href="/api/appserver.php?wsdl">WSDL</a> for the service.
Click on an operation name to view it's details.</p>
<ul><li><a href='#' onclick="popout();popup('apiLogin')">apiLogin</a></li><div id='apiLogin' class='hidden'>
HTML 文件很长,所以我粘贴了一部分。但是在日志的末尾,我收到以下错误消息:
发生错误 NSXMLParserErrorColumn = 14; NSXMLParserErrorLineNumber = 77; NSXMLParserErrorMessage = "AttValue: \" 或 ' 预期\n";
文件名为appserver.php,它应该执行文件并根据调用的API返回XML结果。
正确结果的示例应在演示服务器上进行测试:
"banners":["title":"Images","link":"/index.php?route=product/product&product_id=47","image":"http://domain. comm/image/data/banners/3.png","title":"Testing","link":"Image","image":"http://domain.comm/image/data/banners/ 4.png","title":"Something","link":"/index.php?route=product/product&path=57&product_id=49","image":"http://domain .comm/image/data/banners/6.png","title":"asdgasdg","link":"/index.php?route=product/product&path=57&product_id=49"," image":"http://domain.comm/image/data/banners/5.png","title":"Image","link":"/index.php?route=product/product&path =57&product_id=49","image":"http://domain.comm/image/data/banners/1.png"],"modules":"featured":["product_id":" 47","thumb":"http://domain.com/image/cache/data/hp_1-187.5x187.5.jpg","name":"HP
我再次粘贴部分文件而不是整个文件作为摘要,因此问题不会太长。
我检查了服务器配置,发现服务器上安装了 SOAP、XMLWriter 和 XMLReader,但错误仍然存在。试图解决这个问题 2 周没有结果真的很头疼。如果有人已经经历过并且可以提供建议,我将感谢您的帮助。
【问题讨论】:
首先,标题呢?您是否正确设置了所有需要的标头,例如身份验证、内容类型、接受等 其次,有时php会在500错误或403(未经授权)的情况下发送html。将您的 html 页面保存到 pastebin 并在此处提供链接 感谢您的支持我发布了我的答案,但现在我有一个新错误,某些 API 函数调用在我的服务器上测试时再次返回“文档末尾的 nsxmlparser 额外内容”它工作正常但客户端服务器不是。我已经尝试了所有可能的解决方案,但都没有成功,请问有什么建议吗? 【参考方案1】:经过一段时间的研究,我在客户端的服务器上发现了错误,他将他的服务器配置为强制进行 www 重定向,因此每当请求来自非 www 域时,它应该重定向到 www,我更改了 API URL 调用并添加了“ www",现在它正在加载应用程序。
【讨论】:
以上是关于SOAP API 从服务器返回 HTML 而不是 XML的主要内容,如果未能解决你的问题,请参考以下文章