iOS學習筆記(八)——iOS網路通訊http之NSURLConnection

來源:互聯網
上載者:User

          

 

1)

 

- (void)httpAsynchronousRequest{    NSURL *url = [NSURL URLWithString:@"http://url"];        NSString *post=@"postData";        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    [request setHTTPMethod:@"POST"];    [request setHTTPBody:postData];    [request setTimeoutInterval:10.0];        NSOperationQueue *queue = [[NSOperationQueue alloc]init];    [NSURLConnection sendAsynchronousRequest:request                                       queue:queue                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){                               if (error) {                                   NSLog(@"Httperror:%@%d", error.localizedDescription,error.code);                               }else{                                                                      NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];                                                                      NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];                                                                      NSLog(@"HttpResponseCode:%d", responseCode);                                   NSLog(@"HttpResponseBody %@",responseString);                               }                           }];    }


2)

 

- (void)httpConnectionWithRequest{        NSString *URLPath = [NSString stringWithFormat:@"http://url"];    NSURL *URL = [NSURL URLWithString:URLPath];    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];    [NSURLConnection connectionWithRequest:request delegate:self];    }- (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response{       NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];    NSLog(@"response length=%lld  statecode%d", [response expectedContentLength],responseCode);}// A delegate method called by the NSURLConnection as data arrives.  The// response data for a POST is only for useful for debugging purposes,// so we just drop it on the floor.- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data{    if (mData == nil) {        mData = [[NSMutableData alloc] initWithData:data];    } else {        [mData appendData:data];    }    NSLog(@"response connection");}// A delegate method called by the NSURLConnection if the connection fails.// We shut down the connection and display the failure.  Production quality code// would either display or log the actual error.- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error{        NSLog(@"response error%@", [error localizedFailureReason]);}// A delegate method called by the NSURLConnection when the connection has been// done successfully.  We shut down the connection with a nil status, which// causes the image to be displayed.- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection{    NSString *responseString = [[NSString alloc] initWithData:mData encoding:NSUTF8StringEncoding];     NSLog(@"response body%@", responseString);}

 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;


2、串連失敗,包含失敗。

 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;

 

3、接收資料

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

4、資料接收完畢

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

 

 

AEURLConnection is a simple reimplementation of the API for use on iOS 4. Used properly, it is also guaranteed to be safe against The Deallocation Problem, a thorny threading issue that affects most other networking libraries.

 

 

 

- (void)httpSynchronousRequest{        NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];    NSURLResponse * response = nil;    NSError * error = nil;    NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest                                          returningResponse:&response                                                      error:&error];        if (error == nil)    {        // 處理資料    }}



 

 

 


@張興業TBOW 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.