gpt4 book ai didi

iphone - AFNetworking + SOAP Web服务

转载 作者:行者123 更新时间:2023-12-01 16:55:04 24 4
gpt4 key购买 nike

我有一个使用ASIHttpRequest来使用SOAP Web服务的应用程序,现在我想要(或已经)使用其他网络框架,我选择了AFNetworking,但是看不到如何使用SOAP进行消费,她是我如何使用ASIHttpRequest来进行的:

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

最佳答案

您是否尝试过WSDL2Objc? http://code.google.com/p/wsdl2objc/
我最近在项目中使用了它。它成功地(从消耗的WSDL)生成类以与SOAP服务一起使用。我认为这将是更好的解决方案。

关于iphone - AFNetworking + SOAP Web服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12407517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com