IOS学习之路十八(通过 NSURLConnection 发送 HTTP 各种请求)
2013-08-28 18:13 Lves Li 阅读(413) 评论(0) 编辑 收藏 举报你想通过 Http 协议向服务器发送一个 Get 的包装请求,并在这个请求中添加了一些请
求参数.
向远程服务器发送一个 GET 请求,然后解析返回的数据。通常一个 GET 请求是添加了
一些参数的,这些参数一般是添加在 URL 请求中。
我准备了一个 GET 形式的 webservice 接口,你可以通过 http://pixolity.com/get.php 来进
行请求。
- /* URL = http://pixolity.com/get.php?param1=First¶m2=Second */ NSString *urlAsString = @"http://pixolity.com/get.php";
- urlAsString = [urlAsString stringByAppendingString:@"?param1=First"]; urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];
- NSURL *url = [NSURL URLWithString:urlAsString];
- NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setTimeoutInterval:30.0f];
- [urlRequest setHTTPMethod:@"GET"];
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,
- 
- NSData *data, NSError *error) {
- if ([data length] >0 && error == nil){
- NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"HTML = %@", html); }
- else if ([data length] == 0 && error == nil){
- NSLog(@"Nothing was downloaded."); }
- else if (error != nil){
- NSLog(@"Error happened = %@", error);
- } }];
post请求:
- NSString *urlAsString = @"http://pixolity.com/post.php";
- urlAsString = [urlAsString stringByAppendingString:@"?param1=First"];
- urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];
- NSURL *url = [NSURL URLWithString:urlAsString];
- NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setTimeoutInterval:30.0f];
- [urlRequest setHTTPMethod:@"POST"];
- NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2"; [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]]; NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,
- NSData *data, NSError *error) {
- if ([data length] >0 && error == nil){
- NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"HTML = %@", html); }
- else if ([data length] == 0 && error == nil){
- NSLog(@"Nothing was downloaded."); }
- else if (error != nil){
- NSLog(@"Error happened = %@", error);
- } }];
delete请求:
- NSString *urlAsString = @"http://pixolity.com/delete.php";
- urlAsString = [urlAsString stringByAppendingString:@"?param1=First"];
- urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];
- NSURL *url = [NSURL URLWithString:urlAsString];
- NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
- [urlRequest setTimeoutInterval:30.0f];
- [urlRequest setHTTPMethod:@"DELETE"];
- NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2";
- [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:
- ^(NSURLResponse *response, NSData *data, NSError *error) {
- if ([data length] >0 && error == nil){
- NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"HTML = %@", html); }
- else if ([data length] == 0 && error == nil){
- NSLog(@"Nothing was downloaded."); }
- else if (error != nil){
- NSLog(@"Error happened = %@", error);
- } }];
put请求:
- NSString *urlAsString=@"http://pixolity.com/put.php";
- urlAsString=[urlAsString stringByAppendingString:@"?param1=First"];
- urlAsString=[urlAsString stringByAppendingString:@"¶m2=Second"];
- NSURL *url=[NSURL URLWithString:urlAsString];
- NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
- [urlRequest setTimeoutInterval:30.0f];
- [urlRequest setHTTPMethod:@"PUT"];
- NSString *body=@"bodyParaml=BodyValuel&bodyParam2=BodyValue2";
- [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
- NSOperationQueue *queue=[[NSOperationQueue alloc] init];
- [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
- if ([data length]>0&&error==nil) {
- NSString *html=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"HTML=%@",html);
- }else if([data length]==0&&error==nil){
- NSLog(@"Nothing was downLoaded.");
- }else if(error!=nil){
- NSLog(@"Error Happened=%@",error);
- }
- }];
转载请注明:
本文转自:点击打开链接http://blog.csdn.net/wildcatlele