在Objective-C中调用Magento SOAP API时出错 [英] Getting Error When Calling Magento SOAP API in Objective-C
问题描述
我正在尝试在Objective-C iOS中使用Maginto SOAP API.我可以调用Login API并获取SessionID,但是当我尝试使用相同的SessionID调用其他API时,则会出现以下错误.
I am trying to consume Maginto SOAP API in Objective-C iOS. I can able to call Login API and get SessionID but when I try to call other API's with same SessionID then it is giving me following error.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>5</faultcode><faultstring>Session expired. Try to relogin.</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
我不明白为什么出现此错误消息.如果有人知道解决方案,请帮助我.
I am not getting why this error message is there. If anyone knows the solution then please help me.
谢谢.
这是我的登录代码-
-(void)make_call_to_maginto_api_login{
NSString *soapMessage = @" \
<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"urn:Magento\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> \
<SOAP-ENV:Body> \
<ns1:login> \
<apiUser xsi:type=\"xsd:string\">UserName</apiUser> \
<apiKey xsi:type=\"xsd:string\">Password</apiKey> \
</ns1:login> \
</SOAP-ENV:Body> \
</SOAP-ENV:Envelope>";
NSURL *url = [NSURL URLWithString:@"http://www.hostname.com/index.php/api/v2_soap/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapMessage length]];
[request addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://www.hostname.com/login" forHTTPHeaderField:@"SOAPAction"];
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(connection)
{
self.webData = [NSMutableData data];
}
else
{
NSLog(@"theConnection is null");
}}
如果有人有解决方案,请与我分享,这对我真的很好.谢谢:)
If anyone have solution then please share, it would be really nice to me. Thank you :)
推荐答案
在大量寻找解决方案之后,我已经解决了此问题.我已经对SOAP请求进行了更改,现在可以正常工作了.
After searching lot for solution, I have fixed this issue my own. I have done changes in SOAP request and now its working fine.
谢谢.
这是创建Magento信封请求的方法和触发SOAP请求的方法.
Here is the method which create Magento Envelope request and method which fire SOAP request.
- (NSString *)createEnvelope:(NSString *)method forNamespace:(NSString *)ns forParameters:(NSString *)params{
NSMutableString *s = [NSMutableString string];
[s appendString:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];
[s appendFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"%@\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ns2=\"http://xml.apache.org/xml-soap\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">", ns];
[s appendString:@"<SOAP-ENV:Body>\n"];
[s appendFormat:@"<%@>%@</%@>", method, [params stringByReplacingOccurrencesOfString:@"&" withString:@"&"], method];
[s appendString:@"</SOAP-ENV:Body>"];
[s appendString:@"</SOAP-ENV:Envelope>"];
return s;}
-(void)login_request{
NSString *parameters = [NSString stringWithFormat:@"<username xsi:type=\"xsd:string\">%@</username><apiKey xsi:type=\"xsd:string\">%@</apiKey>",login_name,login_pwd];
NSString *envelope = [self createEnvelope:@"login" forNamespace:@"urn:Magento" forParameters:parameters];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.YOUR_HOST_NAME.com/index.php/api/v2_soap/"]];
//?wsdl=1
[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:Mage_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)
{
self.webData = [NSMutableData data];
}
else
{
NSLog(@"theConnection is null");
}
}
这篇关于在Objective-C中调用Magento SOAP API时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!