0

我正在构建一个使用 API 的应用程序,通过 PHP 它正在工作,但现在我必须在 xcode 中构建并返回错误“格式错误的身份验证标头”,不知道我在 ObjectC 代码中遗漏了什么或如何调试.

在我的 Query.m 中,这就是我构建标题的方式

- (NSData *)multipartFormDataWithBoundary:(NSString *)boundary
{
NSMutableData *body = [NSMutableData data];
// Image data part
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"query[file]\"; filename=\"query.%@\"\r\n", imageType] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/%@\r\n", imageType] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"Content-Transfer-Encoding: binary\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:imageData];
[body appendData:[@"\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
// Location parts
if (self.location) {
    [body appendData:[self textPart:[NSString stringWithFormat:@"%f", location.coordinate.latitude] forKey:@"query[latitude]" boundary:boundary]];
    [body appendData:[self textPart:[NSString stringWithFormat:@"%f", location.coordinate.longitude] forKey:@"query[longitude]" boundary:boundary]];
}
// End of boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]];

return body;
}


#pragma mark -
#pragma mark Instance methods

- (NSString *)create
{
NSURL *queriesURL = [NSURL URLWithString:@"https://url.com/v4/query"];
NSString *contentType = @"multipart/form-data";
NSString *httpMethod = @"POST";
NSString *boundary = [NSString stringWithFormat:@"%d", arc4random() % 999999];  

NSData *contentData = [self multipartFormDataWithBoundary:boundary];

NSString *dateValue = HttpDate();


// request header values
NSString *authorizationValue = [NSString stringWithFormat:@"Authorization: Token %@", accessKey];
NSString *contentTypeValue = [NSString stringWithFormat:@"%@; boundary=%@", contentType, boundary];

// create the request object
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:queriesURL];
[request setHTTPMethod:httpMethod];
[request addValue:contentTypeValue forHTTPHeaderField:@"Content-Type"];
[request addValue:authorizationValue forHTTPHeaderField:@"Authorization"];
[request addValue:dateValue forHTTPHeaderField:@"Date"];
[request setHTTPBody:contentData];

NSLog(@"request: %@", request);

// send the request
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"return data: %@", returnData);

NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"return string: %@", returnString);


return returnString;
}

这就是我在图片中使用的。m

UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
if (originalImage) {

    NSData *jpegData = UIImageJPEGRepresentation(originalImage, 0.75);
    KQuery *query = [[KQuery alloc] initWithImageData:jpegData type:@"jpeg"];
    query.secretKey = @"3r4yq0LI";
    //query.location = locationManager.location; // might be nil
    result = [query create];
    NSLog(@"The return string: %@", [query create]);
}

这是有效的 PHP 查询:

$boundary = uniqid();

# Construct the body of the request
$body  = image_part($boundary, "image", $file_name, $img);
$body .= "--" . $boundary . "--\r\n";

var_dump($body);

$context = stream_context_create(array(
          'http' => array(
               'method' => 'POST',
               'header' => 'Content-type: multipart/form-data; boundary='.$boundary."\r\n" .
                           'Authorization: Token ' . $query_key,
               'content' => $body
               )
          ));

 $result = file_get_contents($url, false, $context);

 echo "Result: ", $result;


 # add image part to a kooaba multipart request
 function image_part($boundary, $attr_name, $file_name, $data) {
 $str  = "--" . $boundary . "\r\n";
 $str .= 'Content-Disposition: form-data; name="'. $attr_name .'"; filename="' .    $file_name . '"' . "\r\n";
 $str .= 'Content-Transfer-Encoding: binary' ."\r\n";
 $str .= 'Content-Type: image/jpeg' . "\r\n\r\n";
 $str .= $data . "\r\n";
 return $str;
}

我想我在 PHP 中使用的所有东西也都在 ObjectC 中......,任何人都知道如何检查我发送的头文件或如何调试它?

4

1 回答 1

0

因此,要回答您的问题,如何调试 ObjC 文件以检查字符串的值,我发现最简单的方法是在使用您设置的变量之前放置一个断点。

因此,在您的示例中,您将在该行上方放置一个断点:

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

一旦模拟器(或真实设备)在断点处暂停,当您将鼠标悬停在变量上时,它们的值就会弹出。

这在 iOS 编码中是一件非常有用的事情。

希望这有帮助。

于 2013-03-08T13:14:26.370 回答