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-CBox2.0ファイルのアップロード-問題

于 2012-12-17T12:21:30.953 に答える
0

私は個人的に、ファイルのアップロードとフォーム データの POST に ASIHTTPRequest を好みます。これはユーザーにとって非常に簡単で、コード サンプルは次のとおりです。

于 2012-12-13T12:58:31.680 に答える