NSMutableRequest 标头错误
我遇到了 NSMutableURLRequest 的问题,我想翻译
curl -b $cookie -F "data=foo" -F "user=bar" http://some/stuff.cgi -H 'X-Requested-With: XMLHttpRequest
我的代码,
NSString *params = [NSString stringWithFormat:@"data=%@&user=bar", aUrl];
NSURL *url = [NSURL URLWithString:[[NSString alloc] initWithFormat:[NSString stringWithFormat:@"http://%@:%d/stuff.cgi", _host, _port]]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:5.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding ]];
[request setValue:[NSString stringWithFormat:@"http://%@/stuff.php", _host] forHTTPHeaderField:@"Referer"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
NSArray *availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", _host]]];
NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies];
[request setAllHTTPHeaderFields:headers];
NSLog(@"headers: %@", [request allHTTPHeaderFields]);
NSURLResponse *response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
...
但它不起作用,我进行搜索但没有结果,我不知道问题出在哪里。
谢谢。
i had a problem with a NSMutableURLRequest, i want to translate this
curl -b $cookie -F "data=foo" -F "user=bar" http://some/stuff.cgi -H 'X-Requested-With: XMLHttpRequest
my code is
NSString *params = [NSString stringWithFormat:@"data=%@&user=bar", aUrl];
NSURL *url = [NSURL URLWithString:[[NSString alloc] initWithFormat:[NSString stringWithFormat:@"http://%@:%d/stuff.cgi", _host, _port]]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:5.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding ]];
[request setValue:[NSString stringWithFormat:@"http://%@/stuff.php", _host] forHTTPHeaderField:@"Referer"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
NSArray *availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", _host]]];
NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies];
[request setAllHTTPHeaderFields:headers];
NSLog(@"headers: %@", [request allHTTPHeaderFields]);
NSURLResponse *response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
...
but it don't work, i'm make search without results and i' don't know where is the problem.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已解决,指定长度即可
Solved, just specifie length