2

我使用以下代码第一次调用magento webservice,它是一个登录名(我使用了这个文档

NSString *soapMessage = @" \
<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
<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> \
            <username xsi:type=\"xsd:string\">user</username> \
            <apiKey xsi:type=\"xsd:string\">password</apiKey> \
        </ns1:login> \
    </SOAP-ENV:Body> \
</SOAP-ENV:Envelope>";

NSURL *url = [NSURL URLWithString:@"http://localhost/magentoPath/api/soap/?wsdl"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];
[request addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"localhost/magentoPath/" 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");

稍后我有以下方法记录我得到的响应。问题是,我总是获得 wsdl 的内容,但我应该从 magento 获得会话 ID。我做错了什么?谢谢!

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Done. received Bytes %d", [self.webData length]);
    NSString *xml = [[NSString alloc] initWithBytes:[self.webData mutableBytes] length:[self.webData length] encoding:NSUTF8StringEncoding];
    NSLog(xml);
}

顺便说一句,请求 xml 来自 PHP 的 SoapClient,登录工作正常。所以它与 Magento 无关。

4

2 回答 2

1

所以我在查看了magento的日志后自己找到了解决方案。它说你不需要每次都声明xml,所以只需删除

<?xml version=\"1.0\" encoding=\"UTF-8\"?>

从请求中,它的工作原理!

于 2012-10-01T14:17:47.337 回答
0

尾随'?wsdl'使服务返回其 WSDL 定义。如果你忽略它,你实际上会调用该服务。

于 2012-10-01T11:01:37.790 回答