我需要为请求设置HTTP标头。在NSURLRequest类的文档中,我没有找到关于HTTP标头的任何信息。我如何设置HTTP头来包含自定义数据?NSURL请求设置HTTP标头
81
A
回答
169
您需要使用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"];
6
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];
0
可以在NSMutableURLRequest
为HeaderField增加价值:
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:VALUE forHTTPHeaderField:@"cookie"];
这是为我工作。
10
为雨燕
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")
0
示例代码
- (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;
}
}];
}
1
我知道它的晚,但可以帮助别人,对于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)
相关问题
- 1. 设置http请求消息标头ios
- 2. 请求HTTP标头
- 3. HTTP请求标头
- 4. 更改http请求标头
- 5. 解析http请求标头
- 6. HTTP请求头
- 7. HTTP头请求
- 8. StringContentProvider是否在HTTP请求中设置Content-Type标头?
- 9. 需要设置所有http请求的默认标头
- 10. 在驱动器请求中设置HTTP标头
- 11. 为所有http请求设置默认标头不起作用
- 12. 无法在$ .ajax请求上设置HTTP主机标头
- 13. 从javascript中设置常规HTTP请求标头
- 14. 在HTTP请求标头中设置LTPA令牌
- 15. 在Angular2 HTTP POST中设置JSON请求标头
- 16. 在Flask测试中为所有请求设置HTTP标头
- 17. 如何为http请求设置“Date”标头
- 18. node-http-proxy设置img请求标题
- 19. 设置标头时HTTParty请求失败
- 20. 在ajax中设置请求标头
- 21. 在Node.js中设置请求标头
- 22. swfupload和设置请求标头
- 23. 在java中设置请求标头
- 24. 如何设置curl的请求标头?
- 25. PHP file_get_contents()和设置请求标头
- 26. 为WebClient请求设置User-Agent标头
- 27. 在硒中设置请求标头
- 28. 在URL中设置请求标头?
- 29. 设置链接的请求标头
- 30. 为Django设置AJAX请求标头
正是我在找的!竖起大拇指! – 2012-09-11 16:50:16
有没有用于添加标题字典的API? – 2014-11-04 17:11:19
优秀的答案!我想指出,RamS的[答](http://stackoverflow.com/a/20760445/1778488)有很好的文档。 – fskirschbaum 2014-12-13 19:25:35