iOS http访问post 、get
2015-12-28 23:48
441 查看
@synthesize nameFiled;//手机号 @synthesize passFiled;//密码 //登录时的按钮事件 - (IBAction) login { //post数据的手机号及密码 NSString *post = [NSString stringWithFormat:@"mobile=%@&password=%@",nameFiled.text,passFiled.text]; //将post数据转换为 NSASCIIStringEncoding 编码格式 NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; //要post的联通网址 [request setURL:[NSURL URLWithString:@"http://client.10010.com/client/login.do"]]; //post类型 [request setHTTPMethod:@"POST"]; //[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; //设置post数据 [request setHTTPBody:postData]; //创建链接 NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (conn) { if (receivedData != nil) { [receivedData release]; } //创建数据接收域 receivedData = [[NSMutableData alloc] initWithData:nil]; } [conn release]; } #pragma mark - #pragma mark Http post 数据 // 每收到一次数据, 会调用一次 - (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data { //将接收的数据进行添加 [receivedData appendData:data]; } // 网络错误时触发 - (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error { //错误的提示 UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:[NSMutableString stringWithFormat:@"%@",error] delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil]; [alert show]; [alert release]; } // 全部数据接收完毕时触发 - (void)connectionDidFinishLoading:(NSURLConnection *)aConn { //将data 转换成为字符串 NSString *results = [[NSString alloc] initWithBytes:[receivedData bytes] length:[receivedData length] encoding:NSUTF8StringEncoding]; //接收的数据 UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:results delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil]; [alert show]; [alert release]; [results release]; }
相关文章推荐
- HTTPS工作原理
- Mac os charles抓包http/https的简单使用介绍(附3.10.1mac破解版下载链接)
- 手机信息的获取(手机IMEI,手机sim卡的IMSI,网络类型(WiFi,LTE(即4G)等),信号强度(蜂窝网络非WiFi),经纬度)
- 基于spark的网络爬虫实现
- Android开启网络adb调试
- HttpURLConnection用法详解
- iOS经典讲解之网络编程 TCP/UDP HTTP
- Android_HttpURLConnection_Get和Post请求
- 网络编程中的spring-servlet.xml文件
- 基于HttpClient4.0的网络爬虫基本框架(Java实现)
- 机器学习公开课笔记(5):神经网络(Neural Network)——学习
- java网络编程中的web.xml配置文件
- 网络爬虫——爬百度贴吧
- HTTP Live Streaming直播(iOS直播)技术分析与实现
- iOS开发之http协议
- Liunx网络异常诊断及修复
- 一步一步实现ListView加载网络数据,下滑底部加载,顶部下拉刷新。并配有双缓存
- TCP粘包
- com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
- http学习01