ios网络请求操作

简介

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文件配置。)

这是因为ios9新增了ATS特性,强制使用https安全协议。

解决此问题需要在info.plist中新增如下操作:

当status code打印200  表示网络请求成功

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值