var param = ""
if self.webUrl.rangeOfString("?") != nil{
param = "&random=\(random)"
}else{
param = "?random=\(random)"
}
let webNSUrl = NSURL(string: (self.webUrl+param))!
if (webNSUrl).host?.rangeOfString("chenzhao.date") != nil{
var is3Gcookie = false
let cookies_1 = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies
if cookies_1 != nil{
for c in cookies_1!{
if c.name == "COOKIE"{
// NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(c)
if c.value == UserInfo.userInstance.jKey{
is3Gcookie = true
}else{
NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(c)
}
}
}
}
if UserInfo.userInstance.key != ""{
if is3Gcookie == false{
let cookieProperties = [NSHTTPCookieName:"COOKIE",
NSHTTPCookieValue:UserInfo.userInstance.key,
NSHTTPCookieDomain:".chenzhao.date",
NSHTTPCookiePath:"/", //mark 位置"/"是域名的全网
// NSHTTPCookieVersion:"0",
]
//
let cookie = NSHTTPCookie(properties:cookieProperties)
// Getdevice.println("手动组成的cookie\(cookie) : path = \(webNSUrl.path!) :host = \(webNSUrl.host!)")
if cookie != nil{
NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(cookie!)
}
}
}
}
let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!)
// Getdevice.println("加载数据设置headers cookies:\(headers)")
let request = NSMutableURLRequest(URL: webNSUrl, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 20)
request.HTTPShouldHandleCookies = true
request.allHTTPHeaderFields = headers
// request.addValue("测试", forHTTPHeaderField: "test")
webView.loadRequest(request)
博客地址http://chenzhao.date/2016/08/30/webCookie-%E8%AE%BE%E7%BD%AE-%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95.html
网友评论