- (void)dataTaskSendGet
{
NSURL *url=[NSURL URLWithString:@"http://192.168.0.124:8080/WYServer/login?username=wyzc&pwd=wyzc"];
NSURLSession *session=[NSURLSession sharedSession];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if (dict[@"error"]) {
NSLog(@"%@",dict[@"error"]);
}
else
{
NSLog(@"%@",dict[@"success"]);
}
} ];
[dataTask resume];
}
- (void)dataTaskSendPost
{
NSURL *url=[NSURL URLWithString:@"http://192.168.0.124:8080/WYServer/login"];
NSString *str=@"username=wyzc&pwd=wyzc";
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
request.HTTPMethod=@"POST";
request.HTTPBody=[str dataUsingEncoding:NSUTF8StringEncoding];
//此处发送千万不能设置,这个地方只发送了口令数据接收者未使用json格式
// [request setValue:@"application/jason" forHTTPHeaderField:@"Content-Type"];
NSURLSession *session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if (dict[@"error"]) {
NSLog(@"%@",dict[@"error"]);
}
else
{
NSLog(@"%@",dict[@"success"]);
}
}];
[dataTask resume];
}
- (void)dataTaskJasonSendDict
{
NSURL *url=[NSURL URLWithString:@"http://192.168.0.124:8080/WYServer/order"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
request.HTTPMethod=@"POST";
//此处发送一定要设置,这个地方把字典封装为json格式
[request setValue:@"application/jason" forHTTPHeaderField:@"Content-Type"];
NSDictionary *dict=@{@"name":@"sd",@"gender":@"man",
@"name":@"sddd",@"gender":@"feman",
@"name":@"xxx",@"gender":@"feman"
};
NSData *data=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
request.HTTPBody=data;
NSURLSession *session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if (dict[@"error"]) {
NSLog(@"%@",dict[@"error"]);
}
else
{
NSLog(@"%@",dict[@"success"]);
}
}];
[dataTask resume];
}