我可能不是唯一一个在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 =&#34; AttValue:\&#34;或者&#39;预期。\ n&#34 ;; }
文件名是appserver.php它应该执行该文件并根据调用的API返回XML结果。
正确结果的示例应在演示服务器上进行测试:
{&#34;横幅&#34;:[{&#34;标题&#34;:&#34;图像&#34;&#34;连结&#34;:&#34;的index.php ?路线=产品/产品&放大器;放大器; PRODUCT_ID = 47&#34;&#34;图像&#34;:&#34; HTTP://domain.comm/image/data/banners/3.png"} ,{&#34;标题&#34;:&#34;测试&#34;&#34;连结&#34;:&#34;图像&#34;&#34;图像&#34;:&#34 ; HTTP://domain.comm/image/data/banners/4.png"},{&#34;标题&#34;:&#34;东西&#34;&#34;连结&#34; :&#34;的index.php路线=产品/产品&放大器;放大器;路径= 57&放大器;放大器; PRODUCT_ID = 49&#34;&#34;图像&#34;:&#34; HTTP://域。通信/图像/数据/横幅/ 6.png&#34;},{&#34;标题&#34;:&#34; asdgasdg&#34;&#34;连结&#34;:&#34; /索引?.PHP路线=产品/产品&放大器;放大器;路径= 57&放大器;放大器; PRODUCT_ID = 49&#34;&#34;图像&#34;:&#34; HTTP://domain.comm/image/data/banners /5.png"},{"title":"Image","link":"/index.php?route=product/产物&放大器;放大器;路径= 57&放大器;放大器; PRODUCT_ID = 49&#34;&#34;图像&#34;:&#34; HTTP://domain.comm/image/data/banners/1.png" }],&#34;模块&#34; {&#34;功能&#34;:[{&#34; PR oduct_id&#34;:&#34; 47&#34;&#34;拇指&#34;:&#34; HTTP://domain.com/image/cache/data/hp_1-187.5x187.5.jpg 34;,&#34;名称&#34;:&#34; HP
我再次粘贴文件的一部分而不是整个文件作为摘要,因此问题不会太久。
我已检查过服务器配置,发现SOAP,XMLWriter和XMLReader已安装在服务器上但仍然存在错误。尝试解决这个问题2周并没有结果真的很头疼。如果有人已经体验过并且可以提供建议,我感谢您的帮助。
答案 0 :(得分:0)
经过一段时间的研究,我在客户端的服务器上发现错误,他配置了他的服务器强制进行www重定向,因此每当有来自非www域的请求时,它应该重定向到www,我更改了API URL打电话并添加&#34; www&#34;现在它正在加载应用程序。