NSURLSession 403 响应
NSURLSession 403 response
我正在使用 NSURLSession 发送 HTTP Post 请求。虽然它没有给出错误,但我在完成处理程序中得到了空响应。请帮我弄清楚我在这里做错了什么。
我的 Url 请求如下所示
- (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];
}
我收到这样的回复
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 ;
真是个愚蠢的错误。我改变了基础 url http://192.168.100.4.8080
为此 http://192.168.100.4:8080
并且它开始工作了。如果有人遇到类似问题,请检查您的 url 和参数。此错误与服务器授权有关。很可能 url.
中有错误
我正在使用 NSURLSession 发送 HTTP Post 请求。虽然它没有给出错误,但我在完成处理程序中得到了空响应。请帮我弄清楚我在这里做错了什么。
我的 Url 请求如下所示
- (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];
}
我收到这样的回复
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 ;
真是个愚蠢的错误。我改变了基础 url http://192.168.100.4.8080
为此 http://192.168.100.4:8080
并且它开始工作了。如果有人遇到类似问题,请检查您的 url 和参数。此错误与服务器授权有关。很可能 url.