2013-05-01 102 views
0

我剛剛通過使用庫AFNetworking(http://afnetworking.com/)從iOS連接到.NET Web服務的教程。我的代碼工作正常,但是,我想知道如何使用本機代碼(如NSURLConnection)來實現相同的功能,默認情況下在iOS?嘗試使用本機iOS代碼連接到.NET Web服務

這裏是我下面的代碼:

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的。

在此先感謝所有回覆的人。

回答

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 
}