package com.mavlushechka.a1qa.utils; import com.mavlushechka.a1qa.driverUtils.HttpURLConnectionFactory; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; public class URLConnectionManager { public static String get(String spec) throws IOException { StringBuilder stringBuilder = new StringBuilder(); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(HttpURLConnectionFactory.createHttpURLConnection(spec, "GET", false).getInputStream()))) { String inputLine; while ((inputLine = bufferedReader.readLine()) != null) { stringBuilder.append(inputLine); } } return stringBuilder.toString(); } public static String post(String spec, String content) throws IOException { HttpURLConnection httpURLConnection = HttpURLConnectionFactory.createHttpURLConnection(spec, "POST", true); StringBuilder stringBuilder = new StringBuilder(); try (DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream())) { dataOutputStream.writeBytes(content); dataOutputStream.flush(); } try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) { String inputLine; while ((inputLine = bufferedReader.readLine()) != null) { stringBuilder.append(inputLine); } } return stringBuilder.toString(); } public static int getResponseCode(String spec, String requestMethod) throws IOException { return HttpURLConnectionFactory.createHttpURLConnection(spec, requestMethod, false).getResponseCode(); } }