iphone - NSURLRequest设置HTTP头

我需要为请求设置HTTP标头。 在NSURLRequest类的文档中,我没有找到任何有关HTTP头的信息。 如何设置HTTP标头以包含自定义数据?

Raphael asked 2019-09-10T23:39:56Z
6个解决方案
179 votes

您需要使用NSMutableURLRequest

NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]
                                autorelease];

[request setValue:VALUE forHTTPHeaderField:@"Field You Want To Set"];

或者添加标题:

[request addValue:VALUE forHTTPHeaderField:@"Field You Want To Set"];
Larry Hipp answered 2019-09-10T23:40:17Z
11 votes

对斯威夫特来说

let url: NSURL = NSURL(string: APIBaseURL + "&login=1951&pass=1234")!
var params = ["login":"1951", "pass":"1234"]
request = NSMutableURLRequest(URL:url)
request.HTTPMethod = "POST"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
Beslan Tularov answered 2019-09-10T23:40:40Z
6 votes
 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"your value" forHTTPHeaderField:@"for key"];//change this according to your need.
[request setHTTPBody:postData];
Ram S answered 2019-09-10T23:40:57Z
1 votes

我知道它很晚但可能会帮助其他人,对于SWIFT 3.0

let url = NSURL(string: "http://www.yourwebsite.com")
    let mutAbleRequest = NSMutableURLRequest(URL: url!)
    mutAbleRequest.setValue("YOUR_HEADER_VALUE", forHTTPHeaderField:"YOUR_HEADER_NAME")
    myWebView.loadRequest(mutAbleRequest)
Umair Khalid answered 2019-09-10T23:41:20Z
0 votes

您可以在NSMutableURLRequest中为HeaderField添加值:

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

[request setValue:VALUE forHTTPHeaderField:@"cookie"];

这对我有用。

Saroj Kumar ojha answered 2019-09-10T23:41:50Z
0 votes

示例代码

 - (void)reqUserBalance:(NSString*)reward_scheme_id id:(NSString*)user_id success:(void (^)(id responseObject))success failure:(void (^)(id responseObject))failure{

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@reward/%@/user/%@/balance",URL_SERVER,reward_scheme_id,user_id]];
    NSLog(@"%@",url);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"true" forHTTPHeaderField:@"Bypass"];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data, NSError *connectionError)
     {
                         options:kNilOptions error:NULL];

         if (data.length > 0 && connectionError == nil)
         {
             NSDictionary * userPoints = [NSJSONSerialization JSONObjectWithData:data
                                                                      options:0
                                                                        error:NULL];

             NSString * points = [[userPoints objectForKey:@"points"] stringValue];
             NSLog(@"%@",points);
             [SecuritySetting sharedInstance].usearAvailablePoints = points;


         }
     }];

}
Binoy jose answered 2019-09-10T23:42:13Z
translate from https://stackoverflow.com:/questions/4809047/nsurlrequest-setting-the-http-header