Ios 如何发送带有HTTP头的PUT请求和带有Objective-C的文件?
我是Objective-C的新手,请让我详细说明我在做什么 我正在尝试使用PUT请求将文件上载到OneDrive PUT请求必须包含 Http头 内容长度7919 内容范围字节0-7918/7919 使用这些头文件将上载一个文件Ios 如何发送带有HTTP头的PUT请求和带有Objective-C的文件?,ios,objective-c,cocoa,onedrive,Ios,Objective C,Cocoa,Onedrive,我是Objective-C的新手,请让我详细说明我在做什么 我正在尝试使用PUT请求将文件上载到OneDrive PUT请求必须包含 Http头 内容长度7919 内容范围字节0-7918/7919 使用这些头文件将上载一个文件 PUT https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337 Content-Length: 7919 Content-Range: bytes 0-7918/7919 <bytes 0-7918 of
PUT https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337
Content-Length: 7919
Content-Range: bytes 0-7918/7919
<bytes 0-7918 of the file>
感谢上帝,我终于找到了一条路。以下是已编辑的工作代码:
-(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);
}
我想你在这里谈论的是HTTP请求?是的@trojanfoe,正如你在屏幕截图中看到的那样,我注意到URL方案是
https://
。你应该让它在标题和文本中更加明显。我还注意到您在Postman和Objective-C代码中使用了不同的内容类型。为什么呢?
-(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);
}