
# HttpClient类说明
#### 路径
- com.roma.apic.livedata.client.v1.HttpClient
- com.huawei.livedata.lambdaservice.livedataprovider.HttpClient
 
#### 说明
使用此类进行HTTP请求。
![](https://support.huaweicloud.com/devg-roma/public_sys-resources/note_3.0-zh-cn.png)
部分HTTP通用响应头不支持返回，如Location等。
#### 使用示例
- com.roma.apic.livedata.client.v1.HttpClient
  ```
  importClass(com.roma.apic.livedata.client.v1.HttpClient);
  importClass(com.roma.apic.livedata.provider.v1.APIConnectResponse);
  function execute(data) {
      var httpClient = new HttpClient();
      var resp = httpClient.request('GET', 'http://apigdemo.exampleRegion.com/api/echo', {}, null, 'application/json');
      myHeaders = resp.headers();
      proxyHeaders = {};
      for (var key in myHeaders) {
          proxyHeaders[key] = myHeaders.get(key);
      }
      return new APIConnectResponse(resp.code(), proxyHeaders, resp.body().string(), false);
  }
  ```
  
- com.huawei.livedata.lambdaservice.livedataprovider.HttpClient
  ```
  importClass(com.huawei.livedata.lambdaservice.livedataprovider.HttpClient);
  function execute(data) {
      var httpExecutor = new HttpClient();
      var obj = JSON.parse(data);
      var host = 'xx.xx.xxx.xx:xxxx';
      var headers = {
          'clientapp' : 'FunctionStage'
      };
      var params = {
          'employ_no' :'00xxxxxx'
      };
      var result = httpExecutor.callGETAPI(host,'/livews/rest/apiservice/iData/personInfo/batch',JSON.stringify(params),JSON.stringify(headers));
      return result;
  }
  ```
  
 
#### 构造器详情
- com.roma.apic.livedata.client.v1.HttpClient **public HttpClient** **()**
  构造一个无参数的HttpClient。
  **public HttpClient(HttpConfig config)**
  构造一个包含[HttpConfig](https://support.huaweicloud.com/devg-roma/apic-dev-190603038.html)配置信息的HttpClient。
  参数：config表示传入HttpClient的配置信息。
  
- com.huawei.livedata.lambdaservice.livedataprovider.HttpClient **public HttpClient** **()**
  构造一个无参数的HttpClient。
  
 
#### 方法列表
- com.roma.apic.livedata.client.v1.HttpClient
  
  | 返回类型             | 方法和说明                                                                                                                                                                                                                              |
  |:---|:---|
  | okhttp3.Response | [request]([HttpConfig](https://support.huaweicloud.com/devg-roma/apic-dev-190603038.html) config) 用于发送rest请求                                              |
  | okhttp3.Response | [request](String method, String url) 通过指定请求方法、请求路径的方式发送rest请求                                                                                              |
  | okhttp3.Response | [request](String method, String url, Map\<String,String\> headers) 通过指定请求方法、请求路径、请求消息头的方式发送rest请求                                                            |
  | okhttp3.Response | [request](String method, String url, Map\<String,String\> headers, String body) 通过指定请求方法、请求路径、请求消息头、请求body体的方式发送rest请求                                 |
  | okhttp3.Response | [request](String method, String url, Map\<String,String\> headers, String body, String contentType) 通过指定请求方法、请求路径、请求消息头、请求body体、contentType的方式发送rest请求 |
     
  ![](https://support.huaweicloud.com/devg-roma/public_sys-resources/note_3.0-zh-cn.png)
  com.roma.apic.livedata.client.v1.HttpClient的响应体必须关闭，未能关闭响应体将导致资源泄漏，以下方式都可以关闭请求体：
  Response.close()
  Response.body().close()
  Response.body().source().close()
  Response.body().charStream().close()
  Response.body().byteStream().close()
  Response.body().bytes()
  Response.body().string()
  
- com.huawei.livedata.lambdaservice.livedataprovider.HttpClient
  
  | 返回类型     | 方法和说明                                                                                                                                                                                         |
  |:---|:---|
  | String   | [callGETAPI](String url) 使用get方法调用http或https服务                                                                    |
  | String   | [callGETAPI](String host, String service, String params, String header) 使用get方法调用http或https服务                         |
  | Response | [get](String url, String header) 使用get方法调用http或https服务                                                                 |
  | String   | [callPostAPI](String host, String service, String content, String header, String contentType) 使用post方法调用http或https服务 |
  | String   | [callPostAPI](String url, String header, String requestBody, String type) 使用post方法调用http或https服务                      |
  | Response | [post](String url, String header, String content, String type) 使用post方法调用http或https服务                              |
  | String   | [callFormPost](String url, String header, String/Map param) formdata格式调用http或https服务                             |
  | Response | [callFormPost](String url, String header, String param, FormDataMultiPart form) formdata格式调用http或https服务            |
  | String   | [callDelAPI](String url, String header, String content, String type) 使用delete方法请求http或https服务                      |
  | String   | [callPUTAPI](String url, String header, String content, String type) 通过put方法调用http或https服务                          |
  | String   | [callPatchAPI](String url, String header, String content, String type) 使用patch方法调用http或https服务                    |
  | Response | [put](String url, String header, String content, String type) 使用put方法调用http或https服务                               |
     
  
 
#### 方法详情
- com.roma.apic.livedata.client.v1.HttpClient
  - **public okhttp3.Response request([HttpConfig](https://support.huaweicloud.com/devg-roma/apic-dev-190603038.html) config)**
    用于发送rest请求
    **输入参数**
    config：传入[HttpConfig](https://support.huaweicloud.com/devg-roma/apic-dev-190603038.html)的配置信息
    **返回信息**
    返回响应的消息体
    
  
  - **public okhttp3.Response request(String method, String url)**
    通过指定请求方法、请求路径的方式发送rest请求
    **输入参数**
    - method：请求方法
    
    - url：请求路径
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public okhttp3.Response request(String method, String url, Map\<String,String\> headers)**
    通过指定请求方法、请求路径、请求消息头的方式发送rest请求
    **输入参数**
    - method：请求方法
    
    - url：请求路径
    
    - headers：Map类型，请求消息头信息
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public okhttp3.Response request(String method, String url, Map\<String,String\> headers, String body)**
    通过指定请求方法、请求路径、请求消息头、请求body体的方式发送rest请求
    **输入参数**
    - method：请求方法
    
    - url：请求路径
    
    - headers：Map类型，请求消息头信息
    
    - body：请求body体信息
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public okhttp3.Response request(String method, String url, Map\<String,String\> headers, String body, String contentType)**
    通过指定请求方法、请求路径、请求消息头、请求body体、contentType的方式发送rest请求
    **输入参数**
    - method：请求方法
    
    - url：请求路径
    
    - headers：Map类型，请求消息头信息
    
    - body：请求body体信息
    
    - contentType：请求体的Content-type类型
    
    
    **返回信息**
    返回响应的消息体
    
   
- com.huawei.livedata.lambdaservice.livedataprovider.HttpClient
  - **public String callGETAPI(String url)**
    使用get方法调用http或https服务
    **输入参数**
    url：服务地址
    **返回信息**
    返回响应的消息体
    
  
  - **public String callGETAPI(String host, String service, String params, String header)**
    使用get方法调用http或https服务
    **输入参数**
    - host：服务地址
    
    - service：服务路径
    
    - params：http参数信息
    
    - header：http头部信息
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public Response get(String url, String header)**
    使用get方法调用http或https服务
    **输入参数**
    - url：服务地址
    
    - header：请求头信息
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public String callPostAPI(String host, String service, String content, String header, String contentType)**
    使用post方法调用http或https服务
    **输入参数**
    - host：服务地址
    
    - service：服务路径
    
    - content：消息体
    
    - header：请求头信息
    
    - contentType：内容类型
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public String callPostAPI(String url, String header, String requestBody, String type)**
    使用post方法调用http或https服务
    **输入参数**
    - url：服务地址
    
    - header：请求头信息
    
    - requestBody：消息体
    
    - type：MIME类型
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public Response post(String url, String header, String content, String type)**
    使用post方法调用http或https服务
    **输入参数**
    - url：服务地址
    
    - header：请求头信息
    
    - content：消息体
    
    - type：MIME类型
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public String callFormPost(String url, String header, String/Map param)**
    formdata格式调用http或https服务
    **输入参数**
    - url：服务地址
    
    - header：请求头信息
    
    - param：参数信息
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public Response callFormPost(String url, String header, String param, FormDataMultiPart form)**
    formdata格式调用http或https服务
    **输入参数**
    - url：服务地址
    
    - header：请求头信息
    
    - param：参数信息
    
    - form：Body参数
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public String callDelAPI(String url, String header, String content, String type)**
    使用delete方法请求http或https服务
    **输入参数**
    - url：服务地址
    
    - header：请求头信息
    
    - content：消息体
    
    - type：MIME类型
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public String callPUTAPI(String url, String header, String content, String type)**
    使用put方法调用http或https服务
    **输入参数**
    - url：服务地址
    
    - header：请求头信息
    
    - content：消息体
    
    - type：MIME类型
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public** **String callPatchAPI(String url, String header, String content, String type)**
    使用patch方法调用http或https服务
    **输入参数**
    - url：服务地址
    
    - header：请求头信息
    
    - content：消息体
    
    - type：MIME类型
    
    
    **返回信息**
    返回响应的消息体
    
  
  - **public Response put(String url, String header, String content, String type)**
    使用put方法调用http或https服务
    **输入参数**
    - url：服务地址
    
    - header：请求头信息
    
    - content：消息体
    
    - type：MIME类型
    
    
    **返回信息**
    返回响应的消息体
    
   
 
