# 1.1 HttpRequest GET

1. 行動裝置最常使用跟網頁或資料庫拿取資料的方式就是透過RestFul api利用Http Method方式去拿取需要的資料
2. 透過Http Request成功後就會回傳對應的Http Response，再確認是XML 或 JSON格式後，再去parse出所需的資料。

## 常用的Http Method有 GET 及 POST兩種。

### 以下為 Http Get (Objective-C) 範例：

```
 void getResponse(){

    // create URL String

    NSString *url =@"http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-061?locationName=內湖區&elementName=AT";


    // change encode URL if your url has Chinese etc...

    NSString *strUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet\]];


    // create http request object.

    NSMutableURLRequest *requestData = [[NSMutableURLRequest alloc] init];

    [requestDataset HTTPMethod:@"GET"];  //set method is "GET"

    [requestDataset URL:[NSURL URLWithString:strUrl]];  //set request URL string.  

    //set http header for "Authorization"

    [requestDatasetValue:@"your token" forHTTPHeaderField:@"Authorization"];


    //create session. 

    NSURLSession *session = [NSURLSession sharedSession];


    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:requestData completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {

    // handle connectivity error here

              if(error) { 

                  NSLog(@"dataTaskWithRequest error: %@", error);

                  return;

              }

    // handle HTTP error code here

             if([response isKindOfClass:[NSHTTPURLResponse class]]) {

                     NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

                     if(statusCode !=200) {

                               NSLog(@"dataTaskWithRequest HTTP status code: %d", statusCode);

                               return;

                      }

              }

     // response data.

     NSLog(@"data: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

    }];

    [dataTaskresume];
}
```

## hint:

* 若URL本身沒有中文或特殊字元，可以直接轉URLString

  ```
    ex: NSString *url =@"http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-061";

       [requestData setURL:[NSURL URLWithString:url ]]; //set request URL string.
  ```
* 若URL 並不是https安全加密連線的URL 在 IOS 9.0之後會報錯

The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.

App Transport Security has blocked a cleartext HTTP (http\://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

若要繼續使用該URL 則須去info.plist 裡面修改設定

1. 加入 dictionary 選項 App Transport Security Setting.

![](https://82571971-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LnKlMLu3Fi9cTfFpxCa%2F-LnKlNyvDuKxVSNfU4eA%2F-LnKlTfUDmKB7lCt0t4C%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-02-08%20%E4%B8%8B%E5%8D%882.06.39.png?generation=1566954614649166\&alt=media)2. 在當中加入子選項 Allow Arbitrary Loads 並將 Type改成Boolean 及 Value改成YES 即可解決

![](https://82571971-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LnKlMLu3Fi9cTfFpxCa%2F-LnKlNyvDuKxVSNfU4eA%2F-LnKlTfWZh5ItNQ4aAUR%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-02-08%20%E4%B8%8B%E5%8D%882.11.49.png?generation=1566954614639078\&alt=media)
