summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/mavlushechka/a1qa/utils/URLConnectionManager.java48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/main/java/com/mavlushechka/a1qa/utils/URLConnectionManager.java b/src/main/java/com/mavlushechka/a1qa/utils/URLConnectionManager.java
new file mode 100644
index 0000000..286df12
--- /dev/null
+++ b/src/main/java/com/mavlushechka/a1qa/utils/URLConnectionManager.java
@@ -0,0 +1,48 @@
+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();
+ }
+
+}