package com.jianrmod.plugins.harmony.Tools;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpTools {
    public void httpGet(final String url, final HttpInterface networkInterface) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    connection.connect();
                    String tmp, str = "";
                    while ((tmp = reader.readLine()) != null) {
                        str += tmp;
                    }
                    reader.close();
                    networkInterface.httpGetData(str, connection.getResponseCode());
                } catch (Exception e) {
                    e.printStackTrace();
                    networkInterface.httpError(e);
                }
            }
        }).start();

    }
}
