package com.rehome.mqttclienttemperature.utils;

import org.apache.commons.io.IOUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;

/**
 * @author huangwenfei
 * @version v1.0.0.0
 * Created DateTime 2021-04-26 9:35
 * @description: http请求工具类
 */
public class WeatherUtil {
    /**
     * @date 2021-04-29 11:23
     * @description: get请求
     * @Param: url get请求的url
     */
	public static String analysisUrl(String url){
        HttpURLConnection httpConnection = null;
        String output = "";
        try {
            URL targetUrl = new URL(url);
            httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("GET");
            httpConnection.setRequestProperty("Content-Type",
                    "application/json");
            InputStreamReader isr = new InputStreamReader(httpConnection
                    .getInputStream(),"utf-8");
            BufferedReader responseBuffer = new BufferedReader(isr);
            output = responseBuffer.readLine();
        } catch (Exception e) {
 
        } finally {
            httpConnection.disconnect();
        }
        return output;
    }
    /**
     * @date 2021-04-29 11:23
     * @description: get请求
     * @Param: url get请求的url
     */
    public static String analysisUrlGzip(String url){
        HttpURLConnection httpConnection = null;
        String output = "";
        try {
            URL targetUrl = new URL(url);
            httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("GET");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            InputStream stream = new GZIPInputStream(httpConnection.getInputStream());
            output = IOUtils.toString(stream,"utf-8");
        } catch (Exception e) {

        } finally {
            httpConnection.disconnect();
        }
        return output;
    }
}
