iOS网络相关iOS学习笔记首页投稿(暂停使用,暂停投稿)

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

2016-03-17  本文已影响1108人  门前有棵葡萄树

封装系统自带的请求

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

上一篇下一篇

猜你喜欢

热点阅读