iOS网络篇---使用NSConnection 实现post和get发送和接收(同步和异步模式)
2015-06-25 19:15
866 查看
- (void)jsonRequestSerial
{
NSURLRequest *request=[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://192.168.0.124:8080/WYServer/login?user=1222&pwd=kj"]];
NSData *data=[NSURLConnectionsendSynchronousRequest:request
returningResponse:nilerror:nil];
NSDictionary *dict=[NSJSONSerializationJSONObjectWithData:data
options:NSJSONReadingAllowFragmentserror:nil];
if (dict[@"error"]) {
NSLog(@"%@",dict[@"error"]);
}
else
{
NSLog(@"%@",dict[@"%@"]);
}
}
- (void) getAsyn
{
NSURL *url=[NSURLURLWithString:@"http://192.168.0.124:8080/WYServer/login?username=333zc&pwd=w444"];
NSURLRequest *request=[NSURLRequestrequestWithURL:url];
[NSURLConnectionsendAsynchronousRequest:request
queue:[[NSOperationQueue
alloc] init]
completionHandler:^(NSURLResponse *response,NSData *data,
NSError *connectionError) {
NSLog(@"Response=%@",response);
NSDictionary *dict=[NSJSONSerializationJSONObjectWithData:data
options:NSJSONReadingAllowFragmentserror:nil];
if (dict[@"error"]) {
NSLog(@"%@",dict[@"error"]);
}
else
{
NSLog(@"%@",dict[@"success"]);
}
}];
}
- (void)postAsyn
{
NSURL *url=[NSURLURLWithString:@"http://192.168.0.124:8080/WYServer/login"];
NSMutableURLRequest *mutRequest=[NSMutableURLRequestrequestWithURL:url];
mutRequest.HTTPMethod=@"POST";
NSString *str=@"username=wxxc&pwd=w11";
mutRequest.HTTPBody=[strdataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnectionsendAsynchronousRequest:mutRequest
queue:[[NSOperationQueue
alloc] init]
completionHandler:^(NSURLResponse *response,NSData *data,
NSError *connectionError) {
NSLog(@"Response=%@",response);
NSDictionary *dict=[NSJSONSerializationJSONObjectWithData:data
options:NSJSONReadingAllowFragmentserror:nil];
if (dict[@"error"]) {
NSLog(@"%@",dict[@"error"]);
}
else
{
NSLog(@"%@",dict[@"success"]);
}
}];
}
{
NSURLRequest *request=[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://192.168.0.124:8080/WYServer/login?user=1222&pwd=kj"]];
NSData *data=[NSURLConnectionsendSynchronousRequest:request
returningResponse:nilerror:nil];
NSDictionary *dict=[NSJSONSerializationJSONObjectWithData:data
options:NSJSONReadingAllowFragmentserror:nil];
if (dict[@"error"]) {
NSLog(@"%@",dict[@"error"]);
}
else
{
NSLog(@"%@",dict[@"%@"]);
}
}
- (void) getAsyn
{
NSURL *url=[NSURLURLWithString:@"http://192.168.0.124:8080/WYServer/login?username=333zc&pwd=w444"];
NSURLRequest *request=[NSURLRequestrequestWithURL:url];
[NSURLConnectionsendAsynchronousRequest:request
queue:[[NSOperationQueue
alloc] init]
completionHandler:^(NSURLResponse *response,NSData *data,
NSError *connectionError) {
NSLog(@"Response=%@",response);
NSDictionary *dict=[NSJSONSerializationJSONObjectWithData:data
options:NSJSONReadingAllowFragmentserror:nil];
if (dict[@"error"]) {
NSLog(@"%@",dict[@"error"]);
}
else
{
NSLog(@"%@",dict[@"success"]);
}
}];
}
- (void)postAsyn
{
NSURL *url=[NSURLURLWithString:@"http://192.168.0.124:8080/WYServer/login"];
NSMutableURLRequest *mutRequest=[NSMutableURLRequestrequestWithURL:url];
mutRequest.HTTPMethod=@"POST";
NSString *str=@"username=wxxc&pwd=w11";
mutRequest.HTTPBody=[strdataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnectionsendAsynchronousRequest:mutRequest
queue:[[NSOperationQueue
alloc] init]
completionHandler:^(NSURLResponse *response,NSData *data,
NSError *connectionError) {
NSLog(@"Response=%@",response);
NSDictionary *dict=[NSJSONSerializationJSONObjectWithData:data
options:NSJSONReadingAllowFragmentserror:nil];
if (dict[@"error"]) {
NSLog(@"%@",dict[@"error"]);
}
else
{
NSLog(@"%@",dict[@"success"]);
}
}];
}
相关文章推荐
- 网络爬虫3-使用LIB_http库
- 网络爬虫2--PHP/CURL库(client URL Request Library)
- HttpServletRequest类的getAttribute()方法与getParameter()方法的区别
- 网络课程视频网站
- 关于 P2P facebookc 等设计网络技术架构概念汇总
- 网络爬虫1-创建第一个简单的脚本
- as3.0加载本地或网络上的图片
- java用httpclient 4.3 实现将base64串进行文件上传
- HTTP长连接
- TCP慢启动、拥塞避免、快速重传、快速回复
- android使用ImageView加载本地SdCard图片和加载网络图片
- 计算机网络总结(二)
- Http请求Demo
- 网络知识点总结(一)
- VMware下ubuntu系统一直莫名其妙地连接无线网络导致Hadoop集群无法正常启动的解决方法
- AFHTTPRequestOperationManager当一个网络请求加入菊花
- iPhone作为热点为PC提供网络
- 下载网络附件
- 怎样在Linux中查看apache是用那个httpd.conf
- http://yjck.iteye.com/blog/1609232