简介
ios网络请求一般使用http中的GET和POST,通常需要使用到几个类:NSURL、NSURLRequst、NSURLConnection
get:从服务端获取一段内容,通常不包含请求体
post:使用客服端提供的数据更新到服务器实体
NSNRL:指定客户端访问哪里的数据资源
NSURLRequst:标识客户端发起的网络请求内容
NSURLConnection:表示与客户端建立的网络连接
NSURLResponse:表示结果
发起一个网络请求
@interface AppDelegate ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//地址数据是错的 在这里只做演示 指定访问的网络地址
NSURL *url = [NSURL URLWithString:@"http://v.juhe.cn/weather/index?format=2&cityname=%E7%A6%8F%E5%B7%9E"];
//表示客户端发起的网络请求内容
NSURLRequest *requst = [NSURLRequest requestWithURL:url];
NSURLConnection *connection =[[NSURLConnection alloc]initWithRequest:requst delegate:self];
//启动网络请求
[connection start];
return YES;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(nonnull NSURLResponse *)response
{
NSLog(@"@",response); //此处接收到到是服务器返回到数据
}
}
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.(大概的意思就是应用程序安全运输了明文HTTP协议(http:/ /)资源负载是不安全的。暂时的异常可以通过您的应用程序的Info.plist文件配置。)
解决此问题需要在info.plist中新增如下操作:
当status code打印200 表示网络请求成功