0
我已經將許多研究中的代碼放在一起。 對twitpic API信息是在這裏:twitpic API未連接(multipart/form-data)objective-c
通過調試,我可以告訴大家,字典是很好的,而且它獲得通過所有方法,請求被提交,但隨後的NSLog方法返回<>。
我不知道我可能會出錯,我不太瞭解多部分/表單數據結構。也許我的連接有問題?
下面的代碼。
-(void)uploadBoth {
//Create dictionary of post arguments
NSArray *keys = [[NSArray alloc] initWithObjects:@"media",@"username",@"password",@"message",nil];
NSArray *objects = [[NSArray alloc] initWithObjects:
imageData, twitterUserSave, twitterPassSave, [NSString stringWithFormat:@"%@",messageView.text], nil];
NSDictionary *keysDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
NSLog(@"%@", keysDict);
//create twitpic photo post
NSURLRequest *twitpicPost = [self twitpicRequest:keysDict withData:imageData];
//send request, return YES if successful
NSURLConnection *twitpicConnection = [[NSURLConnection alloc] initWithRequest: twitpicPost delegate:self];
if (!twitpicConnection) {
NSLog(@"Failed to submit request");
} else {
NSLog(@"Request submitted");
NSData *receivedData = [[NSMutableData data] retain];
NSLog(@"%@", receivedData); // THIS PART RETURNS <>
}
}
的的NSURLRequest部分
-(NSURLRequest *)twitpicRequest:(NSDictionary *)postKeys withData:(NSData *)data {
NSLog(@"got this far");
//create the URL POST Request to twitpic
NSURL *twitpicURL = [NSURL URLWithString:@"http://twitpic.com/api/uploadAndPost"];
NSMutableURLRequest *twitpicPost = [NSMutableURLRequest requestWithURL:twitpicURL];
[twitpicPost setHTTPMethod:@"POST"];
//Add the header info
NSString *stringBoundary = [NSString stringWithString:@"0xAbCdEfGbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[twitpicPost addValue:contentType forHTTPHeaderField: @"Content-Type"];
//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//add key values from the NSDictionary object
NSEnumerator *keys = [postKeys keyEnumerator];
int i;
for (i = 0; i < [postKeys count]; i++) {
NSString *tempKey = [keys nextObject];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
//add data field and file data
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"data\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:data]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//add the body to the post
[twitpicPost setHTTPBody:postBody];
return twitpicPost;
}
啊謝謝,採用第二種方法(我沒看第一)。 現在我收到了來自twitpic的回覆,雖然它是一個錯誤:/關於沒有收到圖像數據。我在字典中打印我的圖像數據,並且它全部存在。也許這是我上傳的方式的問題? – begdev 2009-11-22 13:52:24