gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:55:23 25 4
gpt4 key购买 nike

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

我已经成功地使用访问和请求 token / secret 设置了我的项目,以向服务器 (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 上传文件,或者有任何建议?会有很大的帮助!谢谢

最佳答案

在摆弄代码的排序顺序和 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 协议(protocol) mumbo jumbo(不要忘记重要的\n\r )并以边界结束以标记参数的结尾。

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

希望这对某人有帮助!

关于objective-c - OAuthConsumer - 使用 OAMutableURLRequest 将图像发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7662269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com