【问题标题】:How to call web service using post method with request paramters for the below api如何使用带有以下 api 请求参数的 post 方法调用 Web 服务
【发布时间】:2016-08-01 10:15:34
【问题描述】:

我有一条路:

file:////var/mobile/Containers/Data/Application/5421D684-D466-44F1-AEF5-7390598B5647/Documents/2016-07-29-17-00-12-0.MOV

通过使用此路径,我想使用以下 API 发布我的视频:

139.162.12.178/mediaone/sample/instagram.php?url=<image_path>&image_name=<image_id>

请帮帮我。

【问题讨论】:

    标签: objective-c api rest post get


    【解决方案1】:

    在 iOS 中发送POST 请求非常简单;并且不需要额外的框架。


    POST请求:

    我们首先创建 POST 的 body(我们想发送的内容)为 NSString,然后将其转换为 NSData。

    NSString *post = [NSString stringWithFormat:@"test=Message&this=isNotReal"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    

    接下来,我们读取postData 的length,因此我们可以在请求中传递它。

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    

    现在我们有了想要发布的内容,我们可以创建一个NSMutableURLRequest,并包含我们的postData。

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];
    

    最后,我们可以发送我们的请求,并通过创建一个新的NSURLSession 来阅读回复:

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"requestReply: %@", requestReply);
    }] resume];
    

    【讨论】:

    • 感谢您的快速响应,我必须自定义我的 api
    • 我有一个输出,即视频录制的视频网址,每次我录制视频时,视频名称都会随着时间戳而变化。它就像 instagram 应用程序
    • 请求数据每次都不相同,只是参数名称相同
    • - (void)saveRecordedFile:(NSURL *)recordedFile { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init]; [assetLibrary writeVideoAtPathToSavedPhotosAlbum:recordedFile completionBlock: ^(NSURL *assetURL, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *title; NSString *消息; NSLog(@"%@",recordedFile); }); }];
    • recordedFile 她我正在输出,输出是 file:////var/mobile/Containers/Data/Application/5421D684-D466-44F1-AEF5-7390598B5647/Documents/2016- 07-29-17-00-12-0.MOV 我想在 api 中使用这个输出
    猜你喜欢
    • 2023-03-31
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    相关资源
    最近更新 更多