2018-09-19 17:09:55 +03:00
|
|
|
package ru.gravit.utils;
|
|
|
|
|
2018-12-20 18:45:01 +03:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
import ru.gravit.utils.helper.IOHelper;
|
|
|
|
|
2018-09-19 17:09:55 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
2019-01-08 16:36:05 +03:00
|
|
|
public final class HTTPRequest {
|
2018-12-29 21:58:58 +03:00
|
|
|
private static final int TIMEOUT = 10000;
|
2018-11-14 13:59:55 +03:00
|
|
|
private static final JsonParser parser = new JsonParser();
|
2018-09-19 17:09:55 +03:00
|
|
|
|
|
|
|
public static int sendCrashreport(String strurl, byte[] data) throws IOException {
|
|
|
|
URL url = new URL(strurl);
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
connection.setRequestProperty("Content-Type",
|
|
|
|
"application/x-www-form-urlencoded");
|
|
|
|
connection.setRequestProperty("Content-Length",
|
|
|
|
Integer.toString(data.length));
|
|
|
|
connection.setRequestProperty("Content-Language", "en-US");
|
|
|
|
OutputStream outputStream = connection.getOutputStream();
|
|
|
|
outputStream.write(data);
|
|
|
|
outputStream.close();
|
|
|
|
return connection.getResponseCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int sendCrashreport(String strurl, String data) throws IOException {
|
|
|
|
return sendCrashreport(strurl, data.getBytes(IOHelper.UNICODE_CHARSET));
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:59:55 +03:00
|
|
|
public static JsonElement jsonRequest(JsonElement request, URL url) throws IOException {
|
2018-09-19 17:09:55 +03:00
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
|
if (TIMEOUT > 0)
|
2018-09-22 17:33:00 +03:00
|
|
|
connection.setConnectTimeout(TIMEOUT);
|
2018-09-19 17:09:55 +03:00
|
|
|
|
|
|
|
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), Charset.forName("UTF-8"));
|
|
|
|
writer.write(request.toString());
|
|
|
|
writer.flush();
|
|
|
|
writer.close();
|
|
|
|
|
|
|
|
InputStreamReader reader;
|
|
|
|
int statusCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
if (200 <= statusCode && statusCode < 300)
|
2018-09-22 17:33:00 +03:00
|
|
|
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
|
|
|
|
else
|
|
|
|
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
|
2019-03-22 09:14:29 +03:00
|
|
|
try {
|
|
|
|
return parser.parse(reader);
|
2019-04-03 16:27:40 +03:00
|
|
|
} catch (Exception e) {
|
2019-03-22 09:14:29 +03:00
|
|
|
return null;
|
|
|
|
}
|
2018-09-19 17:09:55 +03:00
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
2019-01-08 16:36:05 +03:00
|
|
|
private HTTPRequest() {
|
|
|
|
}
|
|
|
|
}
|