首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将HTTP头添加到NSURLRequest

将HTTP头添加到NSURLRequest
EN

Stack Overflow用户
提问于 2010-11-24 17:18:08
回答 2查看 61.6K关注 0票数 51

有没有办法将HTTP头添加到NSURLRequest对象中?我过去常常在NSMutableURLRequest中使用以下命令添加它们:

代码语言:javascript
复制
[request addValue:@"PC" forHTTPHeaderField:@"machineName"]
EN

回答 2

Stack Overflow用户

发布于 2015-07-01 22:08:24

已经回答了(感谢这些答案),但这里有一个更简单的示例:

代码语言:javascript
复制
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setValue:@"es" forHTTPHeaderField:@"Accept-Language"];
票数 7
EN

Stack Overflow用户

发布于 2012-07-12 20:31:07

代码语言:javascript
复制
NSString *urlString = @"http://10.28.79.63:3000/testing";

NSMutableURLRequest *imageRequest = [[NSMutableURLRequest alloc] init] ;
[imageRequest setURL:[NSURL URLWithString:urlString]];
[imageRequest setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[imageRequest setValue:contentType forHTTPHeaderField: @"content-Type"]; 
NSMutableData *body = [NSMutableData dataWithCapacity:[imageData length] + 512];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]]; 

//Here u adds the Headers which will be posted in body 

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; content-type: application/octet-stream; name=\"userfile\"; filename=\"inLove.mp4\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[imageRequest setHTTPBody:body];
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:imageRequest 
                                                delegate:self];
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4265123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档