我可能不是唯一一个在Stackoverflow上发布此问题的人,但不幸的是,他们都没有解决我的问题。
我开发了一个通过 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: \" 或 ' expect";}
文件名是AppServer.php它应该执行文件并根据调用的API返回XML结果。
正确结果的示例应在演示服务器上测试:
{">横幅":[{"标题":"图片","链接":"/索引.php?路由=产品/产品&product_id=47","图像":"http://domain.comm/image/data/banners/3.png"},{"标题":"测试","链接":"图像","图像":"http://domain.comm/image/data/banners/4.png"},{"标题":"某物","链接":"/索引.php?路由=产品/产品&路径=57&product_id=49","图像":"http://domain.comm/image/data/banners/6.png"},{"标题":"asdgasdg","链接":"/索引.php?路由=产品/产品&路径=57&product_id=49","图像":"http://domain.comm/image/data/banners/5.png"},{"标题":"图像","链接":"/索引.php?路由=产品/产品&路径=57&product_id=49","图像":"http://domain.comm/image/data/banners/1.png"}],"模块":{"特色":[{"product_id":"47","拇指":"http://domain.com/image/cache/data/hp_1-187.5x187.5.jpg","名称":"HP
同样,我将文件的一部分而不是整个文件粘贴为摘要,因此问题不会太长。
我检查了服务器配置,发现服务器上安装了SOAP,XMLWriter和XMLReader,但错误仍然存在。试图解决这个问题 2 周但没有结果,这真的很令人头疼。如果有人已经经历过并且可以提供建议,我将感谢您的帮助。
经过一段时间的研究,我在客户端的服务器上发现了错误,他将他的服务器配置为强制 www 重定向,因此每当来自非 www 域的请求时,它都应该重定向到 www,我更改了 API URL 调用并添加了"www",现在它正在加载应用程序。