NSMutableRequest 标头错误

发布于 2025-01-01 05:34:05 字数 1760 浏览 1 评论 0原文

我遇到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪淡了忧伤 2025-01-08 05:34:05

已解决,指定长度即可

[request setValue:[[NSString alloc] initWithFormat:@"%d", [params length]] forHTTPHeaderField:@"Content-Length"];

Solved, just specifie length

[request setValue:[[NSString alloc] initWithFormat:@"%d", [params length]] forHTTPHeaderField:@"Content-Length"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文