`
java-mans
  • 浏览: 11435441 次
文章分类
社区版块
存档分类
最新评论

[IPhone开发]发送Http请求(POST GET)的方法

 
阅读更多
来源:代码人生(http://aminby.net/2010/07/iphone-develop-how-to-send-http-request/)

我们知道Http有Get和Post两种方法,我们分开说吧.

另注: 今天讲的方法是同步的请求, 异步的方法我还没试过, 不知道有没有使用异步的需求, 有的话于发上来和大家分享.

1.Get方法

1.1 使用NSMutableURLRequest

[c-sharp]view plaincopy
  1. NSURL*url=[NSURLURLWithString:@"http://aminby.net"];
  2. NSMutableURLRequest*request=[NSMutableURLRequestnew];
  3. [requestsetURL:url];
  4. [requestsetHTTPMethod:@"GET"];
  5. NSHTTPURLRequest*response;
  6. NSData*data=[NSURLConnectionsendSynchronousRequest:request
  7. returningResponse:&responseerror:nil];
  8. [NSString*strRet=[[NSStringalloc]initWithData:dataencoding:NSUTF8String];
  9. NSLog(strRet);
  10. [strRetrelease];

1.2 使用NSString

[c-sharp]view plaincopy
  1. /options有两个枚举,NSMappedRead这个不懂,NSUncachedRead是不缓存
  2. [NSDatadataWithContentsOfURL:(NSURL*)url
  3. options:(NSUInteger)readOptionsMask
  4. error:(NSError**)errorPtr]
  5. //或者
  6. [NSDatadataWithContentsOfURL:(NSURL*)url];

1.2和1.3的方法是缺点是没办法知道response的status,一般是返回200-299之间的数值代表请求成功.我们可以依照这个code来做数据处理, 如果对地址存在很有把握,就可以使用后两种简单的GET方法.

今天查了一下手册,发现NSArray NSDictionary 也有xxxxWithContentsOfURL的方法, 这两个我还没用过, 应该是跟NSData和NSString一样,但具体怎么用我还不清楚.

2.Post方法

2.1 使用NSMutableURLRequest

[c-sharp]view plaincopy
  1. NSURL*url=[NSURLURLWithString:@"http://aminby.net"];
  2. NSMutableURLRequest*request=[NSMutableURLRequestnew];
  3. [requestsetURL:url];
  4. [requestsetHTTPMethod:@"GET"];
  5. [requestaddValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
  6. [requestsetHTTPBody:@"someparam"];
  7. NSHTTPURLRequest*response;
  8. NSData*data=[NSURLConnectionsendSynchronousRequest:request
  9. returningResponse:&responseerror:nil];
  10. [NSString*strRet=[[NSStringalloc]initWithData:dataencoding:NSUTF8String];
  11. NSLog(strRet);
  12. [strRetrelease];

我们知道Http有Get和Post两种方法,我们分开说吧.

另注: 今天讲的方法是同步的请求, 异步的方法我还没试过, 不知道有没有使用异步的需求, 有的话于发上来和大家分享.

1.Get方法

1.1 使用NSMutableURLRequest

[c-sharp]view plaincopy
  1. NSURL*url=[NSURLURLWithString:@"http://aminby.net"];
  2. NSMutableURLRequest*request=[NSMutableURLRequestnew];
  3. [requestsetURL:url];
  4. [requestsetHTTPMethod:@"GET"];
  5. NSHTTPURLRequest*response;
  6. NSData*data=[NSURLConnectionsendSynchronousRequest:request
  7. returningResponse:&responseerror:nil];
  8. [NSString*strRet=[[NSStringalloc]initWithData:dataencoding:NSUTF8String];
  9. NSLog(strRet);
  10. [strRetrelease];

1.2 使用NSString

[c-sharp]view plaincopy
  1. /options有两个枚举,NSMappedRead这个不懂,NSUncachedRead是不缓存
  2. [NSDatadataWithContentsOfURL:(NSURL*)url
  3. options:(NSUInteger)readOptionsMask
  4. error:(NSError**)errorPtr]
  5. //或者
  6. [NSDatadataWithContentsOfURL:(NSURL*)url];

1.2和1.3的方法是缺点是没办法知道response的status,一般是返回200-299之间的数值代表请求成功.我们可以依照这个code来做数据处理, 如果对地址存在很有把握,就可以使用后两种简单的GET方法.

今天查了一下手册,发现NSArray NSDictionary 也有xxxxWithContentsOfURL的方法, 这两个我还没用过, 应该是跟NSData和NSString一样,但具体怎么用我还不清楚.

2.Post方法

2.1 使用NSMutableURLRequest

[c-sharp]view plaincopy
  1. NSURL*url=[NSURLURLWithString:@"http://aminby.net"];
  2. NSMutableURLRequest*request=[NSMutableURLRequestnew];
  3. [requestsetURL:url];
  4. [requestsetHTTPMethod:@"GET"];
  5. [requestaddValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
  6. [requestsetHTTPBody:@"someparam"];
  7. NSHTTPURLRequest*response;
  8. NSData*data=[NSURLConnectionsendSynchronousRequest:request
  9. returningResponse:&responseerror:nil];
  10. [NSString*strRet=[[NSStringalloc]initWithData:dataencoding:NSUTF8String];
  11. NSLog(strRet);
  12. [strRetrelease];
分享到:
评论

相关推荐

    iPhone开发实战.pdf

    iPhone开发实战 iPhone开发 iPhone iPhone4 iPhone开发实战 iPhone开发 iPhone iPhone4

    Iphone开发系列源码——Iphone主题源码

    Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码...

    iPhone开发基础教程电子书

    首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE、开发框架uikit,还有开发语言objective-c)。对于有一定编程经验的人来说,学习...

    Iphone开发系列源码——iPhone版Wordpress源代码

    Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码...

    Iphone开发系列源码——Image图片缩放随着手指

    Iphone开发系列源码——Image图片缩放随着手指Iphone开发系列源码——Image图片缩放随着手指Iphone开发系列源码——Image图片缩放随着手指Iphone开发系列源码——Image图片缩放随着手指Iphone开发系列源码——Image...

    iPhone开发基础教程-PDF版

    iPhone开发,iPhone开发教程,iPhone开发基础教程PDF版

    iPhone开发基础教程

    《iPhone开发基础教程》内容完整丰富,具有较强的通用性,编程领域中各层次读者都能通过《iPhone开发基础教程》快速学习iPhone开发,提高相关技能。iPhone 是一种全新的移动平台,苹果公司为它推出了强大的软件开发...

    iPhone开发入门到精通视频教程

    资源名称:iPhone开发入门到精通视频教程资源目录:【】iOS开发源码系列---工具【】iOS开发源码系列---应用【】iOS开发源码系列---游戏【】iOS开发源码系列---类库与框架【】iOS开发真机测试与发布【】iOS开发视频...

    轻松学iPhone开发

    轻松学iPhone共分3篇。第1篇介绍iPhone的发展、iPhone开发环境以及开发工具的安装过程、iPhone Simulator模拟器

    iphone开发实战

    本书全面探讨了iPhone平台的两种编程方式——Web开发和SDK编程。全在Web开发方面,分别介绍了三个iPhone Web库,即WebKit、iUI和Canvas,并讨论了Web开发环境Dashcode,最后阐述Web应用程序的调试。在SDK开发方面,...

    iPhone游戏开发

    iPhone游戏开发iPhone游戏开发iPhone游戏开发iPhone游戏开发iPhone游戏开发iPhone游戏开发iPhone游戏开发iPhone游戏开发iPhone游戏开发iPhone游戏开发

    IPhone开发

    iphone开发秘籍,移动手机开发,参照软件开发应用

    深入浅出iPhone开发(清晰版,内含中英2个版本的书)

    知名的Head First系列丛书之一,风格与其他Head First系列一脉相承,一定能让读者轻松学会iPhone开发,《深入浅出iPhone开发》是针对iPhone开发的初学者设计的,以几个应用实例的开发为例,循序渐进地对iPhone开发的...

    Iphone开发系列源码——多功能播放器源码

    Iphone开发系列源码——多功能播放器源码Iphone开发系列源码——多功能播放器源码Iphone开发系列源码——多功能播放器源码Iphone开发系列源码——多功能播放器源码Iphone开发系列源码——多功能播放器源码Iphone开发...

    iPhone3开发基础教程

    这是一本很不错的iphone开发入门级教程

    iPhone开发实战

    iPhone开发实战

    深入浅出iPhone开发

    《深入浅出iPhone开发》,本书是针对iPhone开发的初学者设计的,以几个应用实例的开发为例,循序渐进地对iPhone开发的各个方面进行了讲解。

    Iphone开发系列源码——星级评价实现代码

    Iphone开发系列源码——星级评价实现代码Iphone开发系列源码——星级评价实现代码Iphone开发系列源码——星级评价实现代码Iphone开发系列源码——星级评价实现代码Iphone开发系列源码——星级评价实现代码Iphone开发...

    iphone开发视频教程

    资源名称:iphone开发视频教程资源目录:【】iphone开发视频教程第1集 Mac.OS.X,Cocoa,Touch,Objective-C【】iphone开发视频教程第2集 各种基础的类,功能,对象和实例的介绍【】iphone开发视频教程第3集 如何创建你...

    [iphone.开发书籍 高清PDF]iphone3开发基础教程.part3

    [iphone.开发书籍 高清PDF]iphone3开发基础教程.part3

Global site tag (gtag.js) - Google Analytics