package com.mavlushechka.a1qa.utils; import com.mavlushechka.a1qa.constants.RequestMethod; 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, RequestMethod.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, RequestMethod.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, RequestMethod requestMethod) throws IOException { return HttpUrlConnectionFactory.createHttpUrlConnection(spec, requestMethod, false).getResponseCode(); } }