0
  • 我想从 curl Web 服务获取数据,但我的代码不起作用

      NSURL *url = [NSURL URLWithString:delegate.profilePic];
          NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:picUrl];   
         [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
          [req setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
          NSString *token =delegate.Token;
          [req setValue:token forHTTPHeaderField:@"X-Auth-Token"];
         [req setHTTPMethod:@"GET"];
         NSError *error = nil;
         NSURLResponse *response;
       NSData *urlData=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
       NSString *myString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
          NSLog(@"Url Data %@",myString);
    
4

1 回答 1

0

试试这个代码,我用它来获取图像数据

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
dispatch_async(queue, ^{
    NSURL *url = [NSURL URLWithString:delegate.profilePic];
    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
    [req setHTTPMethod:@"GET"];

    NSString *boundary = @"0x0hHai1CanHazB0undar135";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [req addValue:contentType forHTTPHeaderField: @"Content-Type"];
    [req setValue:delegate.Token forHTTPHeaderField:@"X-Auth-Token"];
    NSError *error = nil;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

    dispatch_sync(dispatch_get_main_queue(), ^{

         profilePicView.image=[UIImage imageWithData:urlData];
    });
});
于 2013-10-10T09:35:12.140 回答