1. 程式人生 > >ios url 請求頭新增引數及自定義cookie

ios url 請求頭新增引數及自定義cookie

   通過請求頭可以傳遞 一些web 想要的一些資訊,但是這個方法有些侷限性,如果web 跳轉的時候頭的資訊會訊息,最好的方法就是自定義cookie 傳值

NSURL* url = [NSURLURLWithString:self.url];

NSMutableURLRequest *request = [[NSMutableURLRequestalloc]initWithURL: url];

   NSString *body = [NSString stringWithFormat: @"uuid=%@&sessionid=%@", uuid,sessionid];

   [request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];

   [request setHTTPMethod: @"POST"];

    [self.myWebViewloadRequest:request];

    [self.viewaddSubview:self.myWebView];

  自定義ccokie 傳值

// 定義 cookie 要設定的 host

NSURL* url = [NSURLURLWithString:self.url];

NSURL *cookieHost = [NSURLURLWithString:self.url];

NSDictionary *dic = @{@"uuid":uuid,@"sessionid"

:sessionid,@"feedid":self.feedid,@"softversion":softversion,@"devicetype":deviceId,@"touuid":   self.touuid};

    [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

// 設定 cookie

NSHTTPCookie *cookie = [NSHTTPCookiecookieWithProperties:

                                [NSDictionary

dictionaryWithObjectsAndKeys:

                                 [cookieHost host], NSHTTPCookieDomain,

                                 [cookieHost path], NSHTTPCookiePath,

                                 key,NSHTTPCookieName,

                                 obj,NSHTTPCookieValue,

nil]];

    [[NSHTTPCookieStoragesharedHTTPCookieStorage] setCookie:cookie];

    }];

    [self.myWebViewloadRequest:request];//載入

    [self.viewaddSubview:self.myWebView];