Question

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.

Was it helpful?

Solution

Solved, just specifie length

[request setValue:[[NSString alloc] initWithFormat:@"%d", [params length]] forHTTPHeaderField:@"Content-Length"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top