Code Example

  1.  

1. Code mẫu .Net

using RestSharp;

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Linq;

using System.Text;

  • API Method Get

public static String HTTP_GET(String url, String urlSerivce, String sercet, Dictionary<string, string> param)

        {

            var client = new RestClient(url);

            var request = new RestRequest(urlSerivce, Method.GET);

            request.AddHeader("Authorization", sercet);

            //Add Parameter vào request

            foreach (var item in param)

            {

                       request.AddParameter(item.Key, item.Value);

            }

            var response = client.Execute(request);

            if(response.StatusCode == System.Net.HttpStatusCode.OK)

            {

                        return response.Content;

            }

            else

            {

                       //Xử lý trường hợp kết nối lỗi

                       return "";

            }

 

        }

  • API Method POST

   //jsonObj : object có cấu trúc theo như mô tả

        public static String HTTP_POST(String url, String urlSerivce, String sercet,object jsonObj)

        {

            var client = new RestClient(url);

            var request = new RestRequest(urlSerivce, Method.GET);

            request.AddHeader("Authorization", sercet);

            request.AddHeader("Content-Type", "application/json;charset=UTF-8");

            request.AddBody(jsonObj); // jsonObj sẽ được tự động chuyển thành chuỗi json tương ứng

            var response = client.Execute(request);

            if(response.StatusCode == System.Net.HttpStatusCode.OK)

            {

                return response.Content;

            }

            else

            {

                //Xử lý trường hợp kết nối lỗi

                return "";

            }

 

        }

  1.  

 2. Code mẫu Java

  • API Method Get

public static String HTTP_GET(String url, boolean authorization, String sercet) throws UnsupportedEncodingException {

                        HttpClient httpClient = HttpClientBuilder.create().build();

                        HttpGet httpGet = new HttpGet(url);

                        if (authorization) {

                                    httpGet.setHeader("Authorization", sercet);

                        }

                        String responseString = "";

                        try {

                                    HttpResponse response = httpClient.execute(httpGet);

                                    HttpEntity entity = response.getEntity();

                                    if (entity != null) {

                                                responseString = EntityUtils.toString(entity, "UTF-8");

                                    }

                        } catch (IOException e) {

                                    e.printStackTrace();

                        }

                        System.out.println(responseString);

                        return responseString;

       }

  • API Method POST

public static String HTTP_POST(String url, boolean authorization, String sercet, String dataJson) throws UnsupportedEncodingException {

                        HttpClient httpClient = HttpClientBuilder.create().build();

                        HttpPost httpPost = new HttpPost(url);

                        if (authorization) {

                                    httpPost.setHeader("Authorization", sercet);

                        }

                        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

                        StringEntity httpEntity = new StringEntity(dataJson, "UTF-8");

                        httpPost.setEntity(httpEntity);

                        String responseString = "";

                        try {

                                    HttpResponse response = httpClient.execute(httpPost);

                                    HttpEntity entity = response.getEntity();

                                    if (entity != null) {

                                                responseString = EntityUtils.toString(entity, "UTF-8");

                                    }

                        } catch (IOException e) {

                                    e.printStackTrace();

                        }

                        System.out.println(responseString);

                        return responseString;

}

Danh sách tệp tin đính kèm

  • HCM_LGSP_TracuuthongtinVBQPPL_v1.0.docx