发布带有NSURL/NSURL连接的请求在ios中不起作用



我正试图用一些参数发出post请求,但没有完成,看看

#define kLatestKivaLoansURL [NSURL URLWithString: @"http://url...."]
NSDictionary *params = @{@"medcine": @"xanax", @"lat": @"31.0000",@"long":@"74.0000",@"offset":@"0"};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:kLatestKivaLoansURL];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[self httpBodyForParamsDictionary:params]];
NSLog(@"%@",request);
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (error) {
        NSLog(@"dataTaskWithRequest error: %@", error);
    }
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
        if (statusCode != 200) {
            NSLog(@"Expected responseCode == 200; received %ld", (long)statusCode);
        }
    }
     }];
[task resume];
   - (NSData *)httpBodyForParamsDictionary:(NSDictionary *)paramDictionary
{
  NSMutableArray *parameterArray = [NSMutableArray array];
[paramDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
    NSString *param = [NSString stringWithFormat:@"%@=%@", key, [self percentEscapeString:obj]];
    [parameterArray addObject:param];
}];
NSString *string = [parameterArray componentsJoinedByString:@"&"];
NSLog(@"%@",string);
return [string dataUsingEncoding:NSUTF8StringEncoding];

}

 - (NSString *)percentEscapeString:(NSString *)string
  {
     NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                             (CFStringRef)string,
                                                                             (CFStringRef)@" ",
                                                                             (CFStringRef)@":/?@!$&'()*+,;=",
                                                                                 kCFStringEncodingUTF8));
     NSLog(@"%@",result);
     return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];

 }

错误

  APP[2866:154303] dataTaskWithRequest error: Error Domain=NSURLErrorDomain Code=-1003 "The operation couldn’t be completed. (NSURLErrorDomain error -1003.)" UserInfo=0x7874ef60 {NSErrorFailingURLStringKey=http://URL..., _kCFStreamErrorCodeKey=8, 
 NSErrorFailingURLKey=http://URL...,   _kCFStreamErrorDomainKey=12, NSUnderlyingError=0x78773e70 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1003.)"}

Try:替换以下方法。

- (NSData *)httpBodyForParamsDictionary:(NSDictionary *)paramDictionary
{
NSError *error =nil;
 return  [NSJSONSerialization dataWithJSONObject:paramDictionary
                                                       options:0
                                                         error:&error];
}

这个链接很好地解释了错误代码,AppleDevDoc。以下是关于这个错误的说明:

kCFURLErrorCannotFindHost  = -1003

我认为域名是错误的。

最新更新