iOS https请求跳过验证请求数据

封装系统自带的请求

https.png

我个人根据项目需求封装的代码如下,仅供参考

YWRequestManager.h

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>

/**
 *  成功block
 *
 *  @param xmlStr xml数据
 */
typedef void(^YWSuccessBlock)(NSString *xmlStr);

/**
 *  失败block
 *
 *  @param error 错误对象
 */
typedef void(^YWFailureBlock)(NSError *error);

@interface YWRequestManager : NSObject

- (void)requestStartWithUrl:(NSURL *)url token:(NSString *)token xmlStr:(NSString *)xmlStr success:(YWSuccessBlock)successBlock failure:(YWFailureBlock)failureBlock;

@end
#import "YWRequestManager.h"

@interface YWRequestManager ()<NSURLConnectionDelegate>
{
    //保存数据
    NSMutableData *_downloadData;
    //成功
    YWSuccessBlock _successBlock;
    //失败
    YWFailureBlock _failureBlock;
}

@end

@implementation YWRequestManager

- (instancetype)init{
    self = [super init];
    if (self) {
        _downloadData = [NSMutableData data];
    }
    return self;
}

- (void)requestStartWithUrl:(NSURL *)url token:(NSString *)token xmlStr:(NSString *)xmlStr success:(YWSuccessBlock)successBlock failure:(YWFailureBlock)failureBlock{
    
    _successBlock = [successBlock copy];
    _failureBlock = [failureBlock copy];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
    [request setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"Basic %@", token]forHTTPHeaderField:@"Authorization"];
    NSData *bodyData = [xmlStr dataUsingEncoding:NSUTF8StringEncoding]
    ;
    [request setValue:@(bodyData.length).stringValue forHTTPHeaderField:@"Content-Length"];
    [request  setHTTPBody:bodyData];
    NSData *data = [xmlStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [request setHTTPMethod:@"POST"];
    [NSURLConnection connectionWithRequest:request delegate:self];
}


#pragma mark NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    _downloadData.length = 0;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [_downloadData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    NSString *xmlStr = [[NSString alloc] initWithData:_downloadData encoding:NSUTF8StringEncoding];
    NSLog(@"请求的数据:%@",xmlStr);
    //成功回调
    if(_successBlock){
        _successBlock(xmlStr);
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"请求失败:%@",error);
    //失败回调
    if(_failureBlock){
        _failureBlock(error);
    }
}

//https
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

//https
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
        [[challenge sender] useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        
        [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
    }
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,923评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,372评论 4 61
  • 16即将结束,迎来17年。16这一年里比较让你们记忆犹新的经历有哪些,17又有哪些规划。
    来信以回复阅读 1,733评论 0 0
  • 今天,我读了《狼王梦》这本书。它讲述了母狼紫岚如何培养育三只小狼在大自然挣扎求生存而发展成为狼王的经过,《狼王...
    一凡风顺阅读 1,769评论 0 0
  • 因为有你,一切才变得有意义。 纵使年华不再,想起来时,心上总是那样地温软。 或许今生你和我永远不可能,你的笑容依然...
    铭玥咏全阅读 1,832评论 1 1

友情链接更多精彩内容