简体   繁体   中英

How to call web service using post method with request paramters for the below api

I have an path:

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

By using this path i want to post my video using the below API:

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

Please help me out .

Sending POST requests in iOS is quite easy; and there's no need for an additional framework.


POST Request:

We begin by creating our POST 's body (ergo. what we'd like to send) as an NSString , and converting it to NSData .

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

Next up, we read the postData 's length , so we can pass it along in the request.

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

Now that we have what we'd like to post, we can create an NSMutableURLRequest , and include our 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];

And finally, we can send our request, and read the reply by creating a new 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM