gpt4 book ai didi

ios - 如何使用 Objective-C 发送带有 HTTP HEADERS 和 FILE 的 PUT 请求?

转载 作者:行者123 更新时间:2023-12-03 16:45:36 26 4
gpt4 key购买 nike

我是 Objective-C 的新手,请让我详细说明我在做什么......

我正在尝试使用 PUT 请求将文件上传到 OneDrive。

PUT 请求必须包含

Http header

One Drive Documentation

内容长度7919

内容范围字节0-7918/7919

并使用这些 header 上传文件

PUT https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337
Content-Length: 7919
Content-Range: bytes 0-7918/7919

<bytes 0-7918 of the file>

下面是使用 Chrome POSTMAN 的整个 PUT 请求的屏幕截图

我正在尝试使用以下 header 发出 PUT 请求:

enter image description here

和一个文件

enter image description here

您能否指导我如何使用 Objective-C 来完成上述操作

问题

使用 POSTMAN 一切工作正常,但是当我尝试使用 Objective-C 执行此操作时,文件未上传,并且我得到以下给出的结果:

{"error":{"code":"invalidRequest","message":"Declared fragment length does not match the provided number of bytes"}}

objective-c 代码

-(void)uploadFile:(NSString*)uploadUrl
{
NSString *urlString = uploadUrl;

NSData* file = [NSData dataWithContentsOfFile: [@"/Users/username/Desktop/s.sql" stringByExpandingTildeInPath]];

NSString* fileName = @"s.sql";


//

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"PUT"];


NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request addValue:@"7919" forHTTPHeaderField: @"Content-Length"];
[request addValue:@"bytes 0-7918/7919" forHTTPHeaderField: @"Content-Range"];


NSMutableData *putData = [NSMutableData data];
[putData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// Append the file
[putData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[putData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileupload\"; filename=\"%@\"\r\n", fileName]dataUsingEncoding:NSUTF8StringEncoding]];
[putData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[putData appendData:[NSData dataWithData:file]];



// Close
[putData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// Append
[request setHTTPBody:putData];


NSError *err;
NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];


}

最佳答案

感谢上帝,我终于找到了方法。下面给出的是编辑后的工作代码:

-(void)uploadFile:(NSString*)uploadUrl
{
NSString *urlString = uploadUrl;

NSData* file = [NSData dataWithContentsOfFile: [@"/Users/username/Desktop/s.sql" stringByExpandingTildeInPath]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"PUT"];


[request addValue:@"7919" forHTTPHeaderField: @"Content-Length"];
[request addValue:@"bytes 0-7918/7919" forHTTPHeaderField: @"Content-Range"];


NSMutableData *putData = [NSMutableData data];
[putData appendData:[NSData dataWithData:file]];


// Append
[request setHTTPBody:putData];


NSError *err;
NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(resSrt);
}

关于ios - 如何使用 Objective-C 发送带有 HTTP HEADERS 和 FILE 的 PUT 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33932736/

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