我成功连接到iOS的.NET Web服务,并通过NSLog将响应xml文档打印到控制台。这是我通过AFNetworking Library做的。我现在能够通过NSURLConnection连接到同一个Web服务,但是,我无法提取我的NSMutableData对象中保存的xml文档。 AFNetworking库能够自动执行此操作,这使事情变得更加容易,但我想知道如何使用本机iOS库执行此操作。这是我正在使用的代码:
//This is the code I am using to make the connection to the web service
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
[request addValue:@"http://tempuri.org/Customers" forHTTPHeaderField:@"SOAPAction"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
// Convert your data and set your request's HTTPBody property
NSString *stringData = @"some data";
NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = requestBodyData;
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
以下是我必须从Web服务接收数据的代码:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
NSString *strData = [[NSString alloc]initWithData:_responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@", strData);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
}
/*
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", [operation responseString]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failed");
}];
[operation start];
*/
我只想打印出我收到的整个xml文档,而不一定要解析整个文档。
感谢所有回复的人。
答案 0 :(得分:0)
查看服务器上的帮助文档,它提出了一个示例请求:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Customers xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>
因此,我将该示例请求放在文本文件中,并通过NSURLConnection
启动请求,如下所示:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *filename = [[NSBundle mainBundle] pathForResource:@"soap1-1example" ofType:@"xml"];
NSData *requestBodyData = [NSData dataWithContentsOfFile:filename];
request.HTTPBody = requestBodyData;
[request setHTTPMethod:@"POST"];
[request addValue:@"http://tempuri.org/Customers" forHTTPHeaderField:@"SOAPAction"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection connectionWithRequest:request delegate:self];
注意,我不是在调用start
,因为as the documentation says除非您使用“initWithRequest:delegate:startImmediately:
方法并为NO
参数提供startImmediately
”将自动为您启动。您的原始代码实际上是两次启动连接,这可能会有问题。
无论如何,使用我修改后的代码和上述请求,它使用NSURLConnection
和AFNetworking
都可以正常工作。我得到的反应(美化)是:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<CustomersResponse xmlns="http://tempuri.org/">
<CustomersResult>
<Customer>
<FirstName>Mohammad</FirstName>
<LastName>Azam</LastName>
</Customer>
<Customer>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Customer>
</CustomersResult>
</CustomersResponse>
</soap:Body>
</soap:Envelope>
如果它仍然不适合您,也许您可以分享您提交的请求的详细信息。
顺便说一句,因为这里没有任何东西需要委托方法(例如你没有流式传输,你没有进行任何身份验证等),你可以消除所有NSURLConnectionDataDelegate
方法({{ 1}},didReceiveResponse
等),只需使用didReceiveData
类方法,sendAsynchronousRequest
(在iOS 5及更高版本中):
NSURLConnection