iOS9苹果将原http协议改成了https协议的方法

2025-05-29 0 36

解决方法:

在info.plist 加入key

?

1

2

3

4

5
<key>NSAppTransportSecurity</key>

<dict>

<key>NSAllowsArbitraryLoads</key>

<true/>

</dict>

下面给大家介绍ios中http 和https 协议的访问

最近做个项目,开始采用的是HTTP协议实现客户端和服务器端的交互,后来需要改成https://www.kuaiidc.com/tag/http" title="HTTP">HTTPS协议。在修改的过程中发现了一些问题,解决方案如下:

https://www.kuaiidc.com/tag/http" title="HTTP">HTTP:

?

1

2

3

4

5

6

7

8

9
NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:urlString]];

[request setHTTPMethod:@"GET"];

NSHTTPURLResponse* urlResponse = nil;

NSError *error = [[NSError alloc] init];

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

NSMutableString *result = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog(@"The result string is :%@",result);

https://www.kuaiidc.com/tag/http" title="HTTP">HTTPS

事件触发

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46
{

NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];

//设置请求方式为get

[request setHTTPMethod:@"GET"];

//添加用户会话id

[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];

//连接发送请求

finished = false;

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

//堵塞线程,等待结束

while(!finished) {

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

}

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response

{}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

//[_waitingDialog dismissWithClickedButtonIndex:0 animated:NO];

[connection release];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{

return NO;

}

//下面两段是重点,要服务器端单项HTTPS 验证,iOS 客户端忽略证书验证。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){

[[challenge sender] useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

[[challenge sender] continueWithoutCredentialForAuthenticationChallenge: challenge];

}

}

NSLog(@"get the whole response");

//[receivedData setLength:0];

}

//处理数据

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

}

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS9苹果将原http协议改成了https协议的方法 https://www.kuaiidc.com/93688.html

相关文章

发表评论
暂无评论