iphone学习笔记--使用http与服务器端通信
2011-07-05 11:30
489 查看
与服务器端的通信的方式有两种:1).http 2).socket 3).webservice
下面是在iphone平台上使用http与服务器端通信,费话不说,直接代码。
1. 创建一个基于视图的应用程序,并命名(这里命名为NetDemo),打开NetDemoViewController.h文件,代码如下:
修改之后,保存。
2. 打开NetDemoViewController.m文件,代码实现如下:
3. 选中NetDemoViewController.xib文件,双击打开,添加一UIButton控件,绑定到btn输出口,再给Touch up side绑定btnPressed事件,保存退出。
运行程序,正常情况下OK,打开Consle看输出结果。
下面是在iphone平台上使用http与服务器端通信,费话不说,直接代码。
1. 创建一个基于视图的应用程序,并命名(这里命名为NetDemo),打开NetDemoViewController.h文件,代码如下:
#import <UIKit/UIKit.h> @interface NetDemoViewController : UIViewController{ UIButton *btn; NSMutableData *receviedData; } @property (nonatomic, retain)IBOutlet UIButton *btn; @property (nonatomic, retain) NSMutableData *receviedData; -(IBAction) btnPressed:(id)sender; @end
修改之后,保存。
2. 打开NetDemoViewController.m文件,代码实现如下:
#import "NetDemoViewController.h" @implementation NetDemoViewController @synthesize btn; @synthesize receviedData; - (IBAction)btnPressed:(id)sender{ NSString *param = [NSString stringWithFormat:@"action=%@&username=%@&password=%@",@"login",@"netdemo"]; NSData *postData = [param dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:@"http://www.xxx.com/post.php"]]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:postData]; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if(conn){ if (receviedData != nil ) { [receviedData release]; } receviedData = [[NSMutableData alloc] init]; } [conn release]; } #pragma mark - #pragma mark NSURLConnectionDelegate - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [receviedData appendData:data]; } - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"connection error: %@",[NSMutableString stringWithFormat:@"%@",error]); } - (void) connectionDidFinishLoading:(NSURLConnection *)connection{ NSString *result = nil; @try { result = [[NSString alloc] initWithBytes:[receviedData bytes] length:[receviedData length] encoding:NSUTF8StringEncoding]; NSLog(@"%@",result); } @catch (NSException * e) { NSLog(@"%@",[NSString stringWithFormat:@"%@",e]); } @finally { [result release]; } } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [receviedData release]; [super dealloc]; } @end
3. 选中NetDemoViewController.xib文件,双击打开,添加一UIButton控件,绑定到btn输出口,再给Touch up side绑定btnPressed事件,保存退出。
运行程序,正常情况下OK,打开Consle看输出结果。
相关文章推荐
- 学习笔记--post 相比get 有很多优点,为什么现在的HTTP通信中大多数请求还是使用get?
- android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- iOS学习笔记(八)——iOS网络通信http之NSURLConnection
- python2学习笔记之查看API接口、http的使用
- ios - HTTP学习、使用注意笔记(后续有补充)
- 网络基础学习笔记二------UDP通信之服务器端
- iOS学习笔记(八)——iOS网络通信http之NSURLConnection
- TCP-IP学习笔记七:Netty使用--简单通信编程3
- java学习笔记-----qq项目----在服务器端实现两个客户端的通信的原理
- IPHONE 多线程使用学习笔记
- 我的iphone开发学习笔记(四)使用UIWebView,UISegmentedControl
- angular学习笔记(二十八)-$http(6)-使用ngResource模块构建RESTful架构
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境(转)
- TCP-IP学习笔记五:Netty使用--简单通信编程1
- node.js学习笔记之HTTP---发送服务器端响应流
- iOS学习笔记(八)——iOS网络通信http之NSURLConnection
- 我的 Android 学习笔记-okhttp 的使用(译)
- 我的 Android 学习笔记-Okhttp 的使用(译)
- Ajax学习笔记,原生Ajax,使用XMLHttpRequest读取xml内容