你想通过 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.csdn.net/wildcatlele

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

IOS学习之路十八(通过 NSURLConnection 发送 HTTP 各种请求)的更多相关文章

  1. IOS学习之路十四(用TableView做的新闻客户端展示页面)

    最近做的也个项目,要做一个IOS的新闻展示view(有图有文字,不用UIwebview,因为数据是用webservice解析的到的json数据),自己一直没有头绪,可后来听一个学长说可以用listvi ...

  2. 嵌入式Linux驱动学习之路(十八)LCD驱动

    驱动代码: /************************************************************************* > File Name: lcd ...

  3. IOS学习之路十二(UITableView下拉刷新页面)

    今天做了一个下拉刷新的demo,主要用到了实现的开源框架是:https://github.com/enormego/EGOTableViewPullRefresh 运行结果如下: 实现很简单下载源代码 ...

  4. IOS学习之路十五(UIView 添加背景图片以及加边框)

    怎样给UIview添加背景图片呢很简单,就是先给view添加一个subview,然后设为背景图片: 效果图如下: 很简单直接上代码: //设置内容 self.myTopView.backgroundC ...

  5. IOS学习之路十(仿人人滑动菜单Slide-out Sidebar Menu)

    最近滑动菜单比较流行,像facebook和人人等都在使用滑动菜单,今天做了一个小demo大体效果如下: 这次用了一个开源的项目ECSlidingViewController这个也是一个挺著名的托管在G ...

  6. IOS学习之路十九(JSON与Arrays 或者 Dictionaries相互转换)

    今天写了个json与Arrays 或者 Dictionaries相互转换的例子很简单: 通过 NSJSONSerialization 这个类的 dataWithJSONObject: options: ...

  7. IOS学习之路十六(UItableView 通过Prepare for segue 页面传值)

    当你点击一个UITableView 的section 或者cell的时候希望把值传到另一个页面(页面是通过segue跳转的),可以通过prepareforsegure 方法传值 (我的UITableV ...

  8. IOS开发---菜鸟学习之路--(二十二)-近期感想以及我的IOS学习之路

    在不知不觉当中已经写了21篇内容 其实一开始是没有想些什么东西的 只是买了Air后 感觉用着挺舒服的,每天可以躺在床上,就一台笔记本,不用网线,不用电源,不用鼠标,不用键盘,干干脆脆的就一台笔记本. ...

  9. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

随机推荐

  1. shell 中用管道模拟多线程

    shell 中用管道模拟多线程 这里以两个例子来对比多线程和单进程 单线程的例子 # config.txt在这个例子和多线程的例子中都会用到 [root@ns_10.2.1.242 test]$ ca ...

  2. oracle_导入、导出数据

    逐步整理oracle导入导出数据 1.单表,不同库之间的导入导出 导出exp system/manager@myoracle file=d:\daochu.dmp tables=(table1) sy ...

  3. asp.net 获得客户端 mac 地址

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. asp.net JSON(一)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. Java多线程详解

    Java线程:概念与原理 一.操作系统中线程和进程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程 ...

  6. POJ 2777 Count Color(线段树+位运算)

    题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...

  7. Thrift实践

    Thrift实践:(一)安装 -- 未完待续   1. 新建一个目录,C:\test\thrift-test,里面建2个子文件夹,client-node和sever-csharp,然后把Thrift官 ...

  8. 判断小数点位数不超过2位的JS代码和在删除确认框里面插JS代码

    <script type="text/javascript"> function checkDecimals(){ var decallowed = 2; var re ...

  9. Spring Resource之ResourceLoader

    ResourceLoader接口意味着任何实现的对象都能够返回Resource实例. public interface ResourceLoader { Resource getResource(St ...

  10. 关于easyui的tab,layout,datagrid嵌套的问题

    我的项目使用easyui作为前台的展示框架现在页面中是一个layout布局(分上,左,中)在左边是一些菜单,点击后,在中间部分增加一个tab显示内容而增加的tab里面是显示一些列表数据,列表上面是查询 ...