1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.core.joran.spi;
16
17 import java.io.*;
18 import java.net.HttpURLConnection;
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 import java.nio.charset.StandardCharsets;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 public class HttpUtil {
26
27 URL url;
28
29 public enum RequestMethod {
30 GET,
31 POST;
32 }
33
34
35 HttpURLConnection conn;
36 RequestMethod requestMethod;
37
38 Map<String, String> headerMap = new HashMap<>(2);
39
40 public HttpUtil(RequestMethod requestMethod, URL url) {
41 this.requestMethod = requestMethod;
42 this.url =url;
43 }
44
45 public HttpUtil(RequestMethod requestMethod, String urlStr) throws MalformedURLException {
46 this(requestMethod, new URL(urlStr));
47 }
48
49 Map<String, String> getHeaderMap() {
50 return headerMap;
51 }
52
53 public HttpURLConnection connectTextTxt() {
54 return connectType( "text/txt;charset=utf-8");
55 }
56
57 public HttpURLConnection connectTextPlain() {
58 return connectType("text/plain; charset=utf-8");
59 }
60
61 public HttpURLConnection connectType(String acceptType) {
62 try {
63 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
64 conn.setRequestMethod(requestMethod.name());
65 headerMap.forEach((h, v) -> conn.setRequestProperty(h, v));
66 conn.setRequestProperty("Accept", acceptType);
67
68 if(requestMethod == RequestMethod.POST) {
69 conn.setDoOutput(true);
70 }
71
72 conn.connect();
73 return conn;
74 } catch (IOException e) {
75 e.printStackTrace();
76 return null;
77 }
78 }
79
80 public String readResponse(HttpURLConnection conn) {
81 if(conn == null)
82 return null;
83
84 try {
85 int responseCode = conn.getResponseCode();
86 if(responseCode == HttpURLConnection.HTTP_OK) {
87 return innerReadResponse(conn);
88 } else {
89 System.out.println("status="+ responseCode+ " Failed response");
90 return null;
91 }
92 } catch (IOException e) {
93 System.out.println("url="+ url.toString()+" failed to read status");
94 e.printStackTrace();
95 return null;
96 }
97 }
98
99 private String innerReadResponse(HttpURLConnection conn) {
100 try (InputStream is = conn.getInputStream()) {
101 BufferedReader in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
102 String inputLine;
103 StringBuffer buffer = new StringBuffer();
104
105 while ((inputLine = in.readLine()) != null) {
106 buffer.append(inputLine);
107 }
108 return buffer.toString();
109 } catch (IOException e) {
110 e.printStackTrace();
111 return null;
112 }
113 }
114
115 public boolean post(HttpURLConnection conn, String str) {
116 if (conn == null) {
117 System.out.println("null HttpURLConnection object");
118 return false;
119 }
120
121 if(requestMethod != RequestMethod.POST) {
122 System.out.println("Incorrect request method "+requestMethod.name());
123 return false;
124 }
125
126 try (OutputStream os = conn.getOutputStream()) {
127 OutputStreamWriter wr = new OutputStreamWriter(os);
128 wr.write(str);
129 wr.flush();
130 return true;
131 } catch (IOException e) {
132 e.printStackTrace();
133 return false;
134 }
135 }
136
137 }