[IOS学习]使用URLRequest方法向服务器端发送数据(3 位领导批示)
- 2011-07-5
- 分类:IOS
- 作者:银子
- 1,282 位领导视察
最近的工作中需要在应用程序中与服务器端进行数据通信,只是最简单的POST数据然后存入数据库。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // 数据内容,存入字符串 NSString *requestString = [NSString stringWithFormat:@"&userid=%d", userId]; CCLOG(@"%@", requestString); // 数据内容转换为UTF8编码,第二个参数为数据长度 NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]]; // 请求的URL地址 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://urlrequest.test/test.php"]]; // 设置请求方式 [request setHTTPMethod:@"POST"]; // 设置请求内容 [request setHTTPBody:requestData]; // 设置请求头声明 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; // 执行请求 [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; |
服务器端就非常简章,获取POST数据存入数据库便可。
相关文章
NOTE:本博内容大部分为原创,转载请注明出处。
永久链接:http://www.zdyi.com/ios-urlrequest/633
![[IOS学习]使用URLRequest方法向服务器端发送数据无法保存中文的问题](http://www.zdyi.com/wp-content/plugins/silver-related-posts/nopicture.gif)

























我来逛逛,顺便帮博主顶一下,嘿嘿。
写得不错呀楼主,继续关注!
博主你的博文不错啊,我会继续支持你的