iOS(obj-c)でgetとPOST

Page content

GET

NSString *urlString = [NSString stringWithFormat:@"https://xxxxx.co.jp/test?key1=%@",key1];

    DLog(@"urlString:%@", urlString)

    NSURLSession *urlSession;
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfig.timeoutIntervalForRequest = 5;
    sessionConfig.timeoutIntervalForResource = 20;

    // 処理を単純化させるためにdelegateは使用しませんでした。
    // UI関連の処理を想定していないため、delegateQueueは独自に作成しました。
    urlSession = [NSURLSession sessionWithConfiguration:sessionConfig
                                               delegate:nil
                                          delegateQueue:nil];

    NSLog(@"get start");

    NSMutableURLRequest *request = [NSMutableURLRequest new];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setValue:@"YKTHttpClient" forHTTPHeaderField:@"User-Agent"];
    [request setHTTPMethod:@"GET"];

    // 取得するデータサイズが小さいのでtaskはNSURLSessionDataTaskを使用
    NSURLSessionDataTask *dataTask
            = [urlSession dataTaskWithRequest:request
                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                NSHTTPURLResponse *httpUrlResponse = (NSHTTPURLResponse *) response;
                                DLog(@"Status code: %ld", httpUrlResponse.statusCode);

                                CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) [response textEncodingName]);
                                NSString *body = [[NSString alloc] initWithData:data encoding:CFStringConvertEncodingToNSStringEncoding(encoding)];
                                NSData *restoreData = [[NSData alloc] initWithBase64EncodedString:body options:0];

                                NSString *restoreString = [[NSString alloc] initWithData:restoreData encoding:NSUTF8StringEncoding];
                                dispatch_async(
                                        dispatch_get_main_queue(),
                                        ^{
                                            self.textView.text = restoreString;
                                        }
                                );
                            }];

    [dataTask resume];

POST


NSString *urlString =@"https://xxxx.co.jp/test";
    NSDictionary *params = @{
        @"name": name,
        @"uuid": uuid,
        };
    DLog(@"urlString:%@", urlString)

    // 連想配列として与えられたパラメータをクエリ文字列に変換する
    NSData *query = [self buildQueryWithDictionary: params];
    
    
    NSURLSession *urlSession;
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfig.timeoutIntervalForRequest = 5;
    sessionConfig.timeoutIntervalForResource = 20;

    // 処理を単純化させるためにdelegateは使用しませんでした。
    // UI関連の処理を想定していないため、delegateQueueは独自に作成しました。
    urlSession = [NSURLSession sessionWithConfiguration:sessionConfig
                                               delegate:nil
                                          delegateQueue:nil];

    NSLog(@"get start");

    NSMutableURLRequest *request = [NSMutableURLRequest new];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
    [request setValue:@"iOS" forHTTPHeaderField:@"User-Agent"];
    [request setHTTPMethod:@"POST"];
    [request setValue: @"application/x-www-form-urlencoded"  forHTTPHeaderField: @"Content-Type"];
    [request setValue: [NSString stringWithFormat: @"%lu", (unsigned long)[query length]]  forHTTPHeaderField: @"Content-Length"];
    [request setHTTPBody: query];
    
    // 取得するデータサイズが小さいのでtaskはNSURLSessionDataTaskを使用
    NSURLSessionDataTask *dataTask
            = [urlSession dataTaskWithRequest:request
                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                NSHTTPURLResponse *httpUrlResponse = (NSHTTPURLResponse *) response;
                                DLog(@"Status code: %ld", httpUrlResponse.statusCode);

                                CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) [response textEncodingName]);
                                NSString *body = [[NSString alloc] initWithData:data encoding:CFStringConvertEncodingToNSStringEncoding(encoding)];
                                NSData *restoreData = [[NSData alloc] initWithBase64EncodedString:body options:0];

                                NSString *restoreString = [[NSString alloc] initWithData:restoreData encoding:NSUTF8StringEncoding];
                                dispatch_async(
                                        dispatch_get_main_queue(),
                                        ^{
                                        }
                                );
                            }];

    [dataTask resume];