给特定的NSURLRequest请求添加特定的Cookis

+ (NSString*) startPage {

    return @"http://192.168.7.185:8088/Html5/index.html";

}


NSString* startPage = [[self class] startPage];

    NSURL *appURL = [NSURL URLWithString:startPage];

    NSString* loadErr = nil;

    

    if(![appURL scheme]) {

NSLog(@"appURL scheme nil");

        NSString* startFilePath = [[self class] pathForResource:startPage];

        if (startFilePath == nil) {

            loadErr = [NSString stringWithFormat:@"ERROR: Start Page at '%@/%@' was not found.", [[self class] wwwFolderName], startPage];

            NSLog(@"%@", loadErr);

            appURL = nil;

        } else {

            appURL = [NSURL fileURLWithPath:startFilePath];

        }

    }

    

    if (!loadErr) {

int height = (int)[[UIScreen mainScreenbounds].size.height;

int width = (int)[[UIScreen mainScreen] bounds].size.width;

NSString *strHeight = [NSString stringWithFormat:@"%d", height];

NSString *strWidth = [NSString stringWithFormat:@"%d", width];

NSString *unidstring = [[UIDevice currentDevice] uniqueIdentifier];

NSString *deviceType = [[UIDevice currentDevice] model];

NSString *version = [[UIDevice currentDevice] systemVersion];

NSDictionary *properties =  [NSDictionary dictionaryWithObjectsAndKeys: strHeight, NSHTTPCookieValue, @"me_client-height"

NSHTTPCookieName, @"/" , NSHTTPCookiePath, @"192.168.7.185" , NSHTTPCookieDomain, nil];

NSHTTPCookie *cookie_PD1 = [NSHTTPCookie cookieWithProperties:properties];

NSDictionary *propertiesWidth =  [NSDictionary dictionaryWithObjectsAndKeys: strWidth, NSHTTPCookieValue, @"me_client-Width"

  NSHTTPCookieName, @"/" , NSHTTPCookiePath, @"192.168.7.185" , NSHTTPCookieDomain, nil];

NSHTTPCookie *cookie_PD2 = [NSHTTPCookie cookieWithProperties:propertiesWidth];

        

NSDictionary *propertiesUni =  [NSDictionary dictionaryWithObjectsAndKeys: unidstring, NSHTTPCookieValue@"me_imei",

NSHTTPCookieName, @"/" , NSHTTPCookiePath, @"192.168.7.185" , NSHTTPCookieDomain ,nil];

NSHTTPCookie *cookie_PD3 = [NSHTTPCookie cookieWithProperties:propertiesUni];

        

NSDictionary *propertiesDev =  [NSDictionary dictionaryWithObjectsAndKeys: deviceType, NSHTTPCookieValue, @"me_userggent",

NSHTTPCookieName, @"/" , NSHTTPCookiePath, @"192.168.7.185" , NSHTTPCookieDomain, nil];

NSHTTPCookie *cookie_PD4 = [NSHTTPCookie cookieWithProperties:propertiesDev];

NSDictionary *propertiesVer =  [NSDictionary dictionaryWithObjectsAndKeys: version, NSHTTPCookieValue, @"me_version",

NSHTTPCookieName, @"/" , NSHTTPCookiePath, @"192.168.7.185" , NSHTTPCookieDomain, nil];

NSHTTPCookie *cookie_PD5 = [NSHTTPCookie cookieWithProperties:propertiesVer];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie_PD1];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie_PD2];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie_PD3];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie_PD4];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie_PD5];

        

        NSMutableURLRequest *appReq = [NSMutableURLRequest requestWithURL:appURL cachePolicy:

  NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];

        [self.webView loadRequest:appReq];

    } else {

        NSString* html = [NSString stringWithFormat:@"<html><body> %@ </body></html>", loadErr];

        [self.webView loadHTMLString:html baseURL:nil];

        self.loadFromString = YES;

    }

在设置特定的cookis时,需要注意以下:

1:在NSDictionary设置字典的时候必须包含以下几项:NSHTTPCookieValue, NSHTTPCookieName, NSHTTPCookiePath, NSHTTPCookieDomain。

2:在甚至键值对NSHTTPCookiePath时,value必须是url的Domain。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值