0

我刚刚完成了使用库 AFNetworking ( http://afnetworking.com/ )从 iOS 连接到 .NET Web 服务的教程。我的代码工作正常,但是,我想知道如何使用 iOS 默认提供的本机代码(如 NSURLConnection)实现相同的功能?

下面是我的代码:

NSURL *url = [NSURL URLWithString:@"http://www.blah.com"];

    NSString *soapBody = @"<?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>";


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

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"%@", [operation responseString]);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"failed");

}];

[operation start];

特别是这段代码:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"%@", [operation responseString]);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"failed");

}];

[operation start];

我想知道如何使用 NSURLConnection 替换。

提前感谢所有回复的人。

4

1 回答 1

0

尝试这样的事情

NSURL *url = [NSURL URLWithString:@"http://www.blah.com"];

NSString *soapBody = @"<?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>";

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] init];
(void)[conn initWithRequest:request delegate:self];

使用这些委托方法来获取您的响应或处理错误

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
     //create _responseData variable before only
    _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
    // You can parse the stuff in your instance variable now

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // The request has failed for some reason!
    // Check the error var
}

如果您希望请求是同步的,请尝试以下操作:

NSError * error = nil;

NSURLResposnse *response;

NSData * data = [NSURLConnection sendSynchronousRequest:request
                                          returningResponse:&response
                                                    error:&error];

if (error == nil)
{
    // Parse data here
}
于 2013-05-01T03:17:34.893 回答