J'ai besoin de définir l'en-tête HTTP pour une demande. Dans la documentation de la classe NSURLRequest, je n'ai rien trouvé concernant l'en-tête HTTP. Comment puis-je définir l'en-tête HTTP pour qu'il contienne des données personnalisées?
iphone
objective-c
cocoa
cocoa-touch
ios
Raphael
la source
la source
pour Swift
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")
la source
request.setValue("Some value", forHTTPHeaderField: "Content-Type")
est-ce que cela fonctionne pour vous?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];
la source
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)
la source
Vous pouvez ajouter de la valeur à
NSMutableURLRequest
HeaderField:NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setValue:VALUE forHTTPHeaderField:@"cookie"];
Cela fonctionne pour moi.
la source
Exemple de code
- (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; } }]; }
la source