2012-09-13 167 views
2

我有一個應用程序使用ASIHttpRequest來使用SOAP Web服務,現在我想(或必須)使用其他網絡框架,並且我選擇了AFNetworking,但無法看到如何爲SOAP消耗做什麼,她就是我如何與ASIHttpRequest做:AFNetworking + SOAP Web服務

NSString *operation=[NSString stringWithString:@"search_service"]; 
NSString *xmlNamespace=[NSString stringWithString:@"http://www.xxx.com/wsdl"]; 
NSString *address=[NSString stringWithString:@"http://www.xxx.com/service"]; 
NSString *parameters=[NSString stringWithFormat:@"<param1>%@</param1><param2>%@</param2>", 
         @"val1", 
         @"val2", 
         ]; 

NSString *operatorTag = [NSString stringWithFormat:@"<%@ xmlns=\"%@\">%@</%@>\n", operation, xmlNamespace, parameters, operation]; 

NSString *soapMessage = [NSString stringWithFormat: 
         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
         "<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">\n" 
         " <s:Header>\n" 
         " <To xmlns=\"http://www.w3.org/2005/08/addressing\">%@</To>\n" 
         " <a:Action>http://tempuri.org/IService1/%@</a:Action>\n" 
         " </s:Header>\n" 
         " <s:Body>\n" 
         " %@" 
         " </s:Body>\n" 
         "</s:Envelope>\n", address, operation, operatorTag 
         ]; 

asiRequest = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:address]]; 
[asiRequest setDelegate:self]; 
[asiRequest addRequestHeader:@"application/soap+xml; charset=utf-8" value:@"Content-Type"]; 
[asiRequest setRequestMethod:@"POST"]; 
[asiRequest setPostBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 
[asiRequest startAsynchronous]; 

編輯:這是我嘗試用AFNetworking:

NSString *operationWSDL= @"search_service"; 
NSString *xmlNamespace= @"http://www.xxx.com/wsdl"; 
NSString *address= @"http://www.xxx.com/service"; 
NSString *parameters=[NSString stringWithFormat:@"<param1>%@</param1><param2>%@</param2>",@"val1",@"val2",]; 


NSString *operatorTag = [NSString stringWithFormat:@"<%@ xmlns=\"%@\">%@</%@>\n", operationWSDL, xmlNamespace, parameters, operationWSDL]; 

NSString *soapMessage = [NSString stringWithFormat: 
         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
         "<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">\n" 
         " <s:Header>\n" 
         " <To xmlns=\"http://www.w3.org/2005/08/addressing\">%@</To>\n" 
         " <a:Action>http://tempuri.org/IService1/%@</a:Action>\n" 
         " </s:Header>\n" 
         " <s:Body>\n" 
         " %@" 
         " </s:Body>\n" 
         "</s:Envelope>\n", address, operationWSDL, operatorTag 
         ]; 

NSURL *url = [NSURL URLWithString:@"http://www.xxx.com/service"]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
    [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"application/soap+xml; charset=utf-8",@"Content-Type", nil] body:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 
}]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); 
}]; 
[operation start]; 
+0

[你有什麼嘗試](http://whathaveyoutried.com),並沒有工作?我們在這裏提供幫助,但不在這裏爲您編寫代碼。 – rckoenes

+1

是的,也許我的問題讓你想到這一點。我編輯我的問題與我嘗試做的事情。 –

回答

0

你試過WSDL2Objc? http://code.google.com/p/wsdl2objc/。 我最近在我的項目中使用了它。它成功生成了類(從消費的WSDL)到SOAP服務。我認爲這會是更好的解決方案。

+0

謝謝,我在之前(在其他項目Web服務)嘗試它,它工作得很好,但與此一返回的值是空的,我不能改變服務器中的任何東西,所以我放棄這個解決方案。 –

+6

警告。我嘗試了很少的成功WSDL2Objc。 ARC支持卡在非發佈分支中。生成代碼時,我使用的幾個WSDL會失敗。最後一個版本差不多3年前。 –

+1

嘗試Objective-C ARC和非ARC WSDL自動代碼生成的SudzC。我發現它確實工作得很好。 http://sudzc.com/ –