【问题标题】:OAuthConsumer - Sending image to server with OAMutableURLRequestOAuthConsumer - 使用 OAMutableURLRequest 将图像发送到服务器
【发布时间】:2011-10-05 13:45:33
【问题描述】:

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

我已经成功地使用访问和请求令牌/秘密设置了我的项目,以便向服务器 (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 使用我正在使用的这个库上传文件,或者有什么建议?会有很大帮助!谢谢

【问题讨论】:

    标签: objective-c ios nsurlrequest


    【解决方案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:)];
    

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

    同样重要的是将你的正文内容的长度写入http头'Content-Length',否则请求不会发送任何数据。

    希望这对某人有所帮助!

    【讨论】:

    • 这似乎取代了之前的帖子参数。不为我工作。其他帖子字段必须以某种方式包含在 oauthSignature 中,我在这里看不到类似的东西。你是怎么解决这个问题的?
    • 可以使用 OAAttachment 类吗?还是 OARequestParameter?
    • 我尝试做同样的事情,但在 POST 中传递 JSON 数据。它给了我“signature_invalid”错误。你能指导我吗?
    【解决方案2】:

    设置body后是否调用[urlRequest prepare]?

    在设置body之前调用prepare。这是库中的一个错误/错误功能,因为它期望主体具有诸如 foo=bar&firble=mumble 之类的参数,因此如果您的主体看起来不像那样,它将崩溃。

    【讨论】:

    • 在设置 [urlRequest setHTTPBody:body] 之前执行 [urlRequest prepare] 时仍然会引发异常
    • 您发布了一个答案,上面写着“我设法使它工作”,您在设置正文之前调用了准备。
    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2017-04-12
    相关资源
    最近更新 更多