給特定的NSURLRequest請求添加特定的Cookies

+ (NSString*) startPage {

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

}


NSString* startPage = [[selfclass] 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 = [NSStringstringWithFormat:@"ERROR: Start Page at '%@/%@' was not found.", [[selfclass] wwwFolderName], startPage];

            NSLog(@"%@", loadErr);

            appURL =nil;

        } else {

            appURL = [NSURLfileURLWithPath:startFilePath];

        }

    }

    

    if (!loadErr) {

int height = (int)[[UIScreenmainScreenbounds].size.height;

int width = (int)[[UIScreenmainScreen] bounds].size.width;

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

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

NSString *unidstring = [[UIDevicecurrentDevice] uniqueIdentifier];

NSString *deviceType = [[UIDevicecurrentDevice] model];

NSString *version = [[UIDevicecurrentDevice] systemVersion];

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

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

NSHTTPCookie *cookie_PD1 = [NSHTTPCookiecookieWithProperties:properties];

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

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

NSHTTPCookie *cookie_PD2 = [NSHTTPCookiecookieWithProperties:propertiesWidth];

        

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

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

NSHTTPCookie *cookie_PD3 = [NSHTTPCookiecookieWithProperties:propertiesUni];

        

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

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

NSHTTPCookie *cookie_PD4 = [NSHTTPCookiecookieWithProperties:propertiesDev];

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

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

NSHTTPCookie *cookie_PD5 = [NSHTTPCookiecookieWithProperties:propertiesVer];

[[NSHTTPCookieStoragesharedHTTPCookieStorage] setCookie:cookie_PD1];

[[NSHTTPCookieStoragesharedHTTPCookieStorage] setCookie:cookie_PD2];

[[NSHTTPCookieStoragesharedHTTPCookieStorage] setCookie:cookie_PD3];

[[NSHTTPCookieStoragesharedHTTPCookieStorage] setCookie:cookie_PD4];

[[NSHTTPCookieStoragesharedHTTPCookieStorage] setCookie:cookie_PD5];

        

       NSMutableURLRequest *appReq = [NSMutableURLRequestrequestWithURL:appURLcachePolicy:

 NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];

        [self.webViewloadRequest:appReq];

    } else {

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

        [self.webViewloadHTMLString:html baseURL:nil];

       self.loadFromString =YES;

    }

在設置特定的cookis時,需要注意以下:

1:在NSDictionary設置字典的時候必須包含以下幾項:NSHTTPCookieValue, NSHTTPCookieName, NSHTTPCookiePath, NSHTTPCookieDomain。

2:在甚至鍵值對NSHTTPCookiePath時,value必須是url的Domain。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章