NSURLSession 403 response

时间:2016-08-31 17:47:38

标签: ios objective-c nsurlsession

I'm sending HTTP Post request using NSURLSession. Although it is not giving error but I'm getting null response in completion handler. Please help me to figure out what I'm doing wrong here.

My Url request looks like this

- (void)testUrl {

    NSError *error;
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

   NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.100.4.8080/proposals/addProposal/forJobId/77"]];
    NSLog(@"url : %@",url);
    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];

    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:[NSString stringWithFormat:@"%d",500] forKey:@"offeredQuote"];
    [params setValue:@"2016-09-01T12:25:56.000Z" forKey:@"offeredTimeline"];
    [params setValue:@"2016-08-31T12:26:08.876Z" forKey:@"postingTime"];
    [params setValue:@"cohcih khccoh" forKey:@"proposalDescription"];
    [params setValue:[NSString stringWithFormat:@"%d",122] forKey:@"userID"];


    NSLog(@"params : %@",params);
    [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [urlRequest setHTTPMethod:@"POST"];

    NSData *postData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
    [urlRequest setHTTPBody:postData];

    dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                    NSLog(@"response : %@",response);
                                    NSLog(@"data : %@", data);
                                    if(error == nil)
                                    {
                                        NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

                                        NSError *jsonError;
                                        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
                                                                                                   options:kNilOptions
                                                                                                     error:&jsonError];
                                        NSLog(@"json object ; %@",jsonObject);
                                        NSLog(@"json text ; %@",text);
                                    }
                                    else{
                                        NSLog(@"erro : %@",error);
                                    }
                                }];
    [dataTask resume];
}

I'm getting response like this

response : <NSHTTPURLResponse: 0x7f97a8e18870> { URL: http://192.168.100.4.8080/proposals/addProposal/forJobId/jobId=77 } { status code: 403, headers {
    Connection = close;
    "Content-Encoding" = gzip;
    "Content-Length" = 21;
    "Content-Type" = "text/html; charset=iso-8859-1";
    Date = "Wed, 31 Aug 2016 17:30:08 GMT";
    Server = Apache;
    Vary = "Accept-Encoding";
} }
2016-08-31 23:03:11.051 urlTest[66351:323159] data : <20>
2016-08-31 23:03:11.051 urlTest[66351:323159] json object ; (null)
2016-08-31 23:03:11.051 urlTest[66351:323159] json text ; 

1 个答案:

答案 0 :(得分:3)

这真是个愚蠢的错误。我更改了基本网址http://192.168.100.4.8080 到这个http://192.168.100.4:8080,它开始工作了。如果有人遇到类似问题,请检查您的网址和参数。此错误与服务器授权有关。最有可能的是网址错误。