【问题标题】:Post Request With NSURL/NSURLConnection not working in ios?使用 NSURL/NSURLConnection 发布请求在 ios 中不起作用?
【发布时间】:2014-12-15 04:22:24
【问题描述】:

我正在尝试使用一些参数进行发布请求,但它没有完成,看看

#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.)"}

【问题讨论】:

    标签: ios iphone post nsurlconnection nsurl


    【解决方案1】:

    尝试:替换下面的方法。

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

    【讨论】:

      【解决方案2】:

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

      kCFURLErrorCannotFindHost  = -1003
      

      我认为域名是错误的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-01
        • 2017-05-25
        • 2018-10-06
        • 1970-01-01
        • 2020-09-28
        • 1970-01-01
        相关资源
        最近更新 更多