【问题标题】:Getting Error When Calling Magento SOAP API in Objective-C在 Objective-C 中调用 Magento SOAP API 时出错
【发布时间】:2014-11-03 09:10:24
【问题描述】:

我正在尝试在 Objective-C iOS 中使用 Maginto SOAP API。我可以调用登录 API 并获取 SessionID,但是当我尝试使用相同的 SessionID 调用其他 API 时,它会出现以下错误。

<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>

我不明白为什么会出现此错误消息。如果有人知道解决方案,请帮助我。

提前谢谢你。

这是我的登录代码 -

-(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");
}}

如果有人有解决方案,请分享,这对我来说真的很好。谢谢你:)

【问题讨论】:

  • 你是怎么做到的?请问您如何获得会话ID API?
  • 以前是 SOAP 请求中的问题,请对此进行更改,以便可以正常工作。
  • 谢谢我做到了,你能用soap调用.svc服务吗?
  • 不知道 SOAP 太慢所以我们编写自己的 API 进行交互。如果你愿意,我可以在这里发布我的代码,它可以完美地对抗 Magento SOAP 服务。

标签: ios objective-c magento soap


【解决方案1】:

在大量寻找解决方案后,我自己解决了这个问题。我已经对 SOAP 请求进行了更改,现在它可以正常工作了。

谢谢。

这里是创建 Magento Envelope 请求的方法和触发 SOAP 请求的方法。

- (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:@"&amp;"], 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");
    }
}

【讨论】:

  • 拜托,您能把这些更改放在您的 SOAP 请求中吗?谢谢。
  • 我会在一小时内。
  • 亲爱的 Rincha,我已经更新了我的答案,请参考。它对我很有效,请告诉我您的反馈。
  • 谢谢!这对我也有用。我使用的是 LogNMagento,但我更喜欢使用本机解决方案。
猜你喜欢
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 2020-04-28
  • 2016-09-01
相关资源
最近更新 更多