iOS网络请求实现可以有两种方式:
NSURLConnection和NSURLSession的示例代码
NSURLConnection网络请求:
第一种get请求:
NSString * urlStr = [NSString stringWithFormat:@"http://apps.alicloud.net:6089/flicker_screen/find/detail?appType=%@&sysType=%@&imageResolution=%@",appType, sysType, imageResolution];
NSURL * url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURLResponse *response;
NSError *error;
NSData *JsonData;
[request setURL:url];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
[request setHTTPMethod:@"GET"];
[request setHTTPBody:nil];
[request setTimeoutInterval:30];
JsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//解析
if (JsonData == nil) {
NSLog(@"send request failed: %@", error);
}else{
NSDictionary * dicJson = [NSJSONSerialization JSONObjectWithData:JsonData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dicJson===:%@",dicJson);
/*
dicJson===:{
content = {
"app_type" = wwdc;
"date_time" = "2018-12-06";
"deal_name" = "\U5eb7\U51cc\U6ce2";
id = 151;
imageUrl = "http://apps.alicloud.net:6089/static/flicker_screen/save_images/KUQ4qnZycBJFRSuWamsoxzfY65OErb3i.png";
"image_file_base64" = "<null>";
"image_file_name" = "KUQ4qnZycBJFRSuWamsoxzfY65OErb3i.png";
"image_number" = "";
"image_resolution" = 750x1334;
"source_image_url" = "";
"sys_type" = IosPhone;
};
message = "\U67e5\U8be2\U6210\U529f";
state = 1;
}
*/
NSDictionary * dic = [dicJson valueForKey:@"content"];
NSLog(@"array:%@",dic);
NSString * imageUrlStr = [dic valueForKey:@"imageUrl"];
self.imageUrlStr = imageUrlStr;
NSLog(@"imageUrlStr:%@",imageUrlStr);
}
第二种 post请求
// NSString * urlStr = [NSString stringWithFormat:@"http://apps.alicloud.net:6089/flicker_screen/find/detail?appType=%@&sysType=%@&imageResolution=%@",appType, sysType, imageResolution];
NSString * urlStr = [NSString stringWithFormat:@"http://apps.alicloud.net:6089/flicker_screen/find/detail"];
NSString * bodyStr = [NSString stringWithFormat:@"appType=%@&sysType=%@&imageResolution=%@",appType, sysType, imageResolution];
NSURL * url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURLResponse *response;
NSError *error;
NSData *JsonData;
[request setURL:url];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
[request setHTTPMethod:@"POST"];
[request setHTTPBody:bodyStr];
[request setTimeoutInterval:30];
JsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//解析
if (JsonData == nil) {
NSLog(@"send request failed: %@", error);
}else{
NSDictionary * dicJson = [NSJSONSerialization JSONObjectWithData:JsonData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dicJson===:%@",dicJson);
/*
dicJson===:{
content = {
"app_type" = wwdc;
"date_time" = "2018-12-06";
"deal_name" = "\U5eb7\U51cc\U6ce2";
id = 151;
imageUrl = "http://apps.alicloud.net:6089/static/flicker_screen/save_images/KUQ4qnZycBJFRSuWamsoxzfY65OErb3i.png";
"image_file_base64" = "<null>";
"image_file_name" = "KUQ4qnZycBJFRSuWamsoxzfY65OErb3i.png";
"image_number" = "";
"image_resolution" = 750x1334;
"source_image_url" = "";
"sys_type" = IosPhone;
};
message = "\U67e5\U8be2\U6210\U529f";
state = 1;
}
*/
NSDictionary * dic = [dicJson valueForKey:@"content"];
NSLog(@"array:%@",dic);
NSString * imageUrlStr = [dic valueForKey:@"imageUrl"];
self.imageUrlStr = imageUrlStr;
NSLog(@"imageUrlStr:%@",imageUrlStr);
}
类似于这种post请求,之前是允许在链接上传参数的,现在苹果为了更安全,已经禁止这种方式了,只能在HTTPBody中传参数
NSURLSession网络请求:
NSURL *url=[NSURL URLWithString:desturl];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:15];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"samli 🐒---data = %@---error = %@",data,error);
NSString *string_content = [[NSString alloc] initWithCString:gsvr encoding:NSASCIIStringEncoding]; //vpassportal:180/res.php
WriteLog("the secondRequest string_content:%s == gsvr=%s",[string_content UTF8String],gsvr);
NSString *desturl =[NSString stringWithFormat:@"https://%@:%d%@",string_content,gPort,@"/ipass00/http/localhost/mobileclientappcontrol"];
NSURL *url=[NSURL URLWithString:desturl];
NSMutableURLRequest *secondRequest = [[NSMutableURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10];
NSString *gfaSession = [[NSString alloc] initWithCString:gcDealSessionID encoding:NSASCIIStringEncoding];
NSString *gfaSession_ =[NSString stringWithFormat:@"%s=%@",SESSION_KEY,gfaSession];
[request setHTTPMethod:@"GET"];
[request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
[request setValue:gfaSession_ forHTTPHeaderField:@"Cookie"];
NSURLSessionDataTask *secondTask = [session dataTaskWithRequest:secondRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"samli 🐒🐒---data = %@---error = %@",data,error);
g_iarrPortHeadSize = 0;
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"the result=%@",result);
if ([self L4Proxy_DeviceAuth])
{
//此判断直接返回 0,不知道为啥
return;
}
int len =[result length];
for (int i= 0; i<len; i++)
{
//根据 返回值的长度 来做循环
//NSRange searchRange = [];
NSString *nsTemp =[self getBetweenStr:result withBeginStr:@"L," withEndStr:@"\r\n"];
if (nsTemp ==nil)
{
break;
}
Source_Split_By_Punctuation([nsTemp UTF8String],",",i);
NSRange r;
int templen = [result length] -[nsTemp length] - 4;
if (templen < 0)
break;
r.location = [nsTemp length] + 4;
r.length = templen;
NSString *nsResult = [result substringWithRange:r];
result = nsResult;
}
iRet = 0;
isLoop = YES;
[gfaSession release];
[string_content release];
[secondRequest release];
}];
[secondTask resume];
[request release];
}];
[task resume];
END
网友评论