您的位置:首页 > 理论基础 > 计算机网络

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"]);
}

}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: