【问题标题】:how to send the values in body when I call the asynchronous post request当我调用异步发布请求时如何发送正文中的值
【发布时间】:2019-07-29 11:15:56
【问题描述】:

我正在尝试执行一项我完全不知道的任务,即在目标 c 中将视频上传到服务器。我是初学者,我用的是swift,但现在我的要求是目标c。

在这里,我必须使用多部分表单数据向具有三个值的服务器发送异步请求。这是需要在正文中发送的数据:

body->form-data
projectId : String
sessionId : String
file : file

它在这一行崩溃了

" [urlRequest addValue:@"65" forHTTPHeaderField:@"projectId"];"

谁能帮我做这个。

提前致谢。

NSString *str = [NSString stringWithFormat:@"http://abcdefghijkl.mnopqrst.com:8965/upload"];
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[urlRequest addValue:@"65" forHTTPHeaderField:@"projectId"];
[urlRequest addValue:@"43" forHTTPHeaderField:@"sessionId"];
[urlRequest addValue:@"" forHTTPHeaderField:@"file"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (error) {
        NSLog(@"Error,%@", [error localizedDescription]);
    } else {
        NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
    }
}];

【问题讨论】:

    标签: ios nsurlconnection objective-c-2.0


    【解决方案1】:

    你应该使用NSMutableURLRequest

    NSMutableURLRequest 是 NSURLRequest 的子类,它允许你 更改请求的属性。

    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    [mutableRequest addValue:@"65" forHTTPHeaderField:@"projectId"];
    ...
    
    request = [mutableRequest copy];
    

    【讨论】:

    • Thankyou ...crash 已清除,但需要使用多部分表单数据,而且我不应该使用任何第三方发送 url 请求。
    猜你喜欢
    • 2019-10-08
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    相关资源
    最近更新 更多