0

我使用 AFNetworking 进行网络操作

那么向https://api.box.com/2.0/files/content发送请求的正确方法是什么?现在我收到 HTTP//1.1 500 Internal Server Error

代码

NSMutableData *body = [NSMutableData data];
NSString *boundaryString = [NSString stringWithFormat:@"rn-------------------------%.0frn", [[NSDate date] timeIntervalSince1970]];
NSData *boundary = [boundaryString dataUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:kBoxNetFileUploadURLFormat];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[BoxNet attachAuthHeaderForRequest:request];

[request addValue:[BoxNet SHA1:content] forHTTPHeaderField:@"Content-MD5"];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=\"%@\"", boundaryString] forHTTPHeaderField:@"Content-Type"];


[body appendData:boundary];

[body appendData:[@"Content-Disposition: form-data; name=\folder_id\"" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[folderID dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:boundary];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; filename=\"%@\"; name=\"filename\"", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:content];

[body appendData:boundary];

[request setHTTPBody:body];
[request setValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField:@"Content-Length"];


AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    completion(JSON, nil);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *err, id JSON) {
    completion(JSON, err);
}];

[operation start];
4

2 回答 2

1

问题是Wrong boundary & parameters formatting,请求处理是一样的。

附上新代码:

NSMutableData *body = [NSMutableData data];
NSString *boundary = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];

NSURL *url = [NSURL URLWithString:@"https://api.box.com/2.0/files/content"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[BoxNet attachAuthHeaderForRequest:request];

[request addValue:[BoxNet SHA1:content] forHTTPHeaderField:@"Content-MD5"];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];

// add file body and filename
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data;filename=\"%@\";name=\"filename\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type:application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:content];

// folder id
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition:form-data;name=\"folder_id\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[folderID dataUsingEncoding:NSASCIIStringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

由于这篇文章,我已经解决了我的问题:Objective-C Box 2.0 File Upload - Problems

于 2012-12-17T12:21:30.953 回答
0

我个人更喜欢 ASIHTTPRequest 用于上传文件和 POST 表单数据,它对用户来说非常东方,这里是代码示例。

于 2012-12-13T12:58:31.680 回答