OAuthConsumer - 使用OAMutableURLRequest将图像发送到服务器

时间:2011-10-05 13:45:33

标签: objective-c ios nsurlrequest

我正在使用Jon Crosby的OAuthConsumer库(google code link

我已成功设置了一个访问& amp;请求令牌/秘密,以便与服务器(GET)进行基本的授权http请求。但是现在我需要创建一个将图像上传到服务器的调用...(POST)

据我所知,我需要自己定义整个httpBody(如果我错了,请纠正我)。我在某个地方发现了一些将数据附加到httpBody的示例代码,但我完全不知道我在做什么。我这样做了:

// create url request
urlRequest = [[[OAMutableURLRequest alloc]  initWithURL:urlToServerGoesHere 
                                            consumer:authentication.consumer
                                            token:authentication.token
                                            realm:nil
                                            signatureProvider:nil] autorelease];
// apply http method on request
[urlRequest setHTTPMethod:@"POST"];

// define boundary and content-type of file in header of request
NSString* boundary = @"----IMAGE_UPLOAD";
NSString* contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[urlRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];

// Create entire body
NSMutableData* body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"upload.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[urlRequest setHTTPBody:body];

在上面的代码末尾应用httpBody时,OAMutableRequest中会抛出异常。我在想,我做错了什么或者没有设定。它似乎与我不使用的参数数组相关......?

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** Call stack at first throw:(0   CoreFoundation                      0x00ddc5a9 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x00f30313 objc_exception_throw + 44
2   CoreFoundation                      0x00dd21cc -[__NSArrayI objectAtIndex:] + 236
3   TwooWrapper                         0x0000d310 -[OAMutableURLRequest parameters] + 610
4   TwooWrapper                         0x0000d45d -[OAMutableURLRequest _signatureBaseString] + 39
5   TwooWrapper                         0x0000c619 -[OAMutableURLRequest prepare] + 213
6   TwooWrapper                         0x0000c17c -[OADataFetcher 

有没有人有关于如何使用我正在使用的库或任何建议通过Oauth上传文件的一些信息?会是一个很大的帮助! THX

2 个答案:

答案 0 :(得分:1)

在摆弄代码的排序顺序和http请求的正文后,我设法让它工作。 这是它的样子:

// create URL from string
NSURL* url = [[NSURL alloc] initWithString:path];

// create url request
urlRequest = [[[OAMutableURLRequest alloc]  initWithURL:url 
                                            consumer:authentication.consumer
                                            token:authentication.token
                                            realm:nil
                                            signatureProvider:nil] autorelease];
[urlRequest prepare];
[urlRequest setHTTPMethod:@"POST"];

// define boundary and content-type of file in header of request
NSString* boundary = @"----IMAGE_UPLOAD";
NSString* contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[urlRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];

// Create entire body
NSMutableData* dataRequestBody = [NSMutableData data];

[dataRequestBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[dataRequestBody appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"upload.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[dataRequestBody appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[dataRequestBody appendData:[NSData dataWithData:imageData]];
[dataRequestBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// add 'type' parameter to request body
[dataRequestBody appendData:[[NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"type\"\r\n\r\npublic\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// Write end-boundary!
[dataRequestBody appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[urlRequest setHTTPBody:dataRequestBody];

// write content length of body in header!
[urlRequest setValue:[[NSNumber numberWithInt:[dataRequestBody length]] stringValue] forHTTPHeaderField:@"Content-Length"];

// do call
dataFetcher = [[OADataFetcher alloc]init];
[dataFetcher fetchDataWithRequest:urlRequest delegate:self didFinishSelector:@selector(onApiSuccess:rawData:) didFailSelector:@selector(onApiFail:rawData:)];

我还添加了两行代码,为名为'type'的httpbody添加一个额外的参数,其值为'public'。如果你查看正在添加的数据,首先插入边界,然后是一些http协议mumbo jumbo(不要忘记重要的\ n \ r)并用边界结束以标记参数的结尾。 / p>

同样重要的是将您身体内容的长度写入http标题'Content-Length',否则请求将不会发送任何数据。

希望这有助于某人!

答案 1 :(得分:0)

设置正文后是否正在调用[urlRequest prepare]?

在设置正文之前调用prepare。它是库中的一个错误/错误,因为它期望身体有像foo = bar& firble = mumble这样的参数,所以如果你的身体看起来不像那样会崩溃。