IOS学习之路十八(通过 NSURLConnection 发送 HTTP 各种请求)

时间:2023-03-09 18:01:53
IOS学习之路十八(通过 NSURLConnection 发送 HTTP 各种请求)

你想通过 Http 协议向服务器发送一个 Get 的包装请求,并在这个请求中添加了一些请

求参数.

向远程服务器发送一个 GET 请求,然后解析返回的数据。通常一个 GET 请求是添加了

一些参数的,这些参数一般是添加在 URL 请求中。
我准备了一个 GET 形式的 webservice 接口,你可以通过 http://pixolity.com/get.php 来进

行请求。

  1. /* URL = http://pixolity.com/get.php?param1=First¶m2=Second */ NSString *urlAsString = @"http://pixolity.com/get.php";
  2. urlAsString = [urlAsString stringByAppendingString:@"?param1=First"]; urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];
  3. NSURL *url = [NSURL URLWithString:urlAsString];
  4. NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setTimeoutInterval:30.0f];
  5. [urlRequest setHTTPMethod:@"GET"];
  6. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  7. [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,
  8. NSData *data, NSError *error) {
  9. if ([data length] >0 && error == nil){
  10. NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  11. NSLog(@"HTML = %@", html); }
  12. else if ([data length] == 0 && error == nil){
  13. NSLog(@"Nothing was downloaded."); }
  14. else if (error != nil){
  15. NSLog(@"Error happened = %@", error);
  16. } }];

post请求:

  1. NSString *urlAsString = @"http://pixolity.com/post.php";
  2. urlAsString = [urlAsString stringByAppendingString:@"?param1=First"];
  3. urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];
  4. NSURL *url = [NSURL URLWithString:urlAsString];
  5. NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setTimeoutInterval:30.0f];
  6. [urlRequest setHTTPMethod:@"POST"];
  7. NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2"; [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]]; NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  8. [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,
  9. NSData *data, NSError *error) {
  10. if ([data length] >0 && error == nil){
  11. NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  12. NSLog(@"HTML = %@", html); }
  13. else if ([data length] == 0 && error == nil){
  14. NSLog(@"Nothing was downloaded."); }
  15. else if (error != nil){
  16. NSLog(@"Error happened = %@", error);
  17. } }];

delete请求:

  1. NSString *urlAsString = @"http://pixolity.com/delete.php";
  2. urlAsString = [urlAsString stringByAppendingString:@"?param1=First"];
  3. urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];
  4. NSURL *url = [NSURL URLWithString:urlAsString];
  5. NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
  6. [urlRequest setTimeoutInterval:30.0f];
  7. [urlRequest setHTTPMethod:@"DELETE"];
  8. NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2";
  9. [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  10. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  11. [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:
  12. ^(NSURLResponse *response, NSData *data, NSError *error) {
  13. if ([data length] >0 && error == nil){
  14. NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  15. NSLog(@"HTML = %@", html); }
  16. else if ([data length] == 0 && error == nil){
  17. NSLog(@"Nothing was downloaded."); }
  18. else if (error != nil){
  19. NSLog(@"Error happened = %@", error);
  20. } }];

put请求:

  1. NSString *urlAsString=@"http://pixolity.com/put.php";
  2. urlAsString=[urlAsString stringByAppendingString:@"?param1=First"];
  3. urlAsString=[urlAsString stringByAppendingString:@"¶m2=Second"];
  4. NSURL *url=[NSURL URLWithString:urlAsString];
  5. NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
  6. [urlRequest setTimeoutInterval:30.0f];
  7. [urlRequest setHTTPMethod:@"PUT"];
  8. NSString *body=@"bodyParaml=BodyValuel&bodyParam2=BodyValue2";
  9. [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  10. NSOperationQueue *queue=[[NSOperationQueue alloc] init];
  11. [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  12. if ([data length]>0&&error==nil) {
  13. NSString *html=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  14. NSLog(@"HTML=%@",html);
  15. }else if([data length]==0&&error==nil){
  16. NSLog(@"Nothing was downLoaded.");
  17. }else if(error!=nil){
  18. NSLog(@"Error Happened=%@",error);
  19. }
  20. }];

转载请注明:

本文转自:点击打开链接http://blog.****.net/wildcatlele

新浪微博:http://weibo.com/u/3202802157