· 7 years ago · Apr 25, 2019, 08:48 AM
1package com.gost_group.nsi.util.http;
2
3import java.io.BufferedReader;
4import java.io.InputStreamReader;
5import java.io.OutputStreamWriter;
6import java.net.HttpURLConnection;
7import java.net.URL;
8
9/**
10 * @author *******@gmail.com
11 * <p>
12 * Nov 17, 2016, 4:40:51 AM
13 */
14public class HttpUtils {
15
16 public static String postAndWait(String data, String urlStr) throws Throwable
17 {
18 // TODO: перделать rv на StringBuffer Ð´Ð»Ñ ÑкороÑти
19 String rv = "";
20 URL url = new URL(urlStr.trim());
21 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
22 conn.setRequestMethod("POST");
23 conn.setRequestProperty("User-Agent", HttpUtils.class.getName());
24 conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
25 conn.setDoOutput(true);
26 conn.setDoInput(true);
27 OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
28 writer.write(data);
29 writer.flush();
30 String line;
31 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
32 while ((line = reader.readLine()) != null) {
33 rv += line;
34 }
35 writer.close();
36 reader.close();
37
38 rv = "{\"status\": \"" + conn.getResponseCode() + "\", \"message\": \"" + conn.getResponseMessage() + "\", \"data\" : " + rv + "}";
39 return rv;
40 }
41
42 /** Делает Ð·Ð°Ð¿Ñ€Ð¾Ñ Ð¸ возвращает тело ответа либо Exception еÑли код не равен 200
43 *
44 * @param data
45 * @param urlStr
46 * @return
47 * @throws Throwable
48 */
49 public static String postAndGetResBody(String data, String urlStr) throws Throwable
50 {
51 return submitAndGetResBody("POST", data, urlStr);
52 }
53
54 /** */
55 public static String submitAndGetResBody(String method, String data, String urlStr) throws Throwable
56 {
57 // TODO: перделать rv на StringBuilder Ð´Ð»Ñ ÑкороÑти
58 String rv = "";
59 URL url = new URL(urlStr.trim());
60 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
61 conn.setRequestMethod(method);
62 conn.setRequestProperty("User-Agent", HttpUtils.class.getName());
63 conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
64 conn.setDoOutput(true);
65 conn.setDoInput(true);
66 if (data != null)
67 {
68 OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
69 writer.write(data);
70 writer.flush();
71 writer.close();
72 }
73 String line;
74 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
75 while ((line = reader.readLine()) != null) {
76 rv += line;
77 }
78 reader.close();
79 if (conn.getResponseCode() != 200)
80 throw new Exception("URL request failed with " + conn.getResponseCode() + " code.");
81 return rv;
82 }
83
84 public static String get(String urlStr) throws Throwable {
85 URL url = new URL(urlStr.trim());
86 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
87 conn.setRequestMethod("GET");
88 conn.setRequestProperty("User-Agent", HttpUtils.class.getName());
89 conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
90 conn.setDoOutput(true);
91 conn.setDoInput(true);
92 int code = conn.getResponseCode();
93 if (code == 200) {
94 String line;
95 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
96 StringBuilder sb = new StringBuilder();
97 while ((line = reader.readLine()) != null) {
98 sb.append(line);
99 }
100 reader.close();
101 return sb.toString();
102 } else {
103 throw new RuntimeException("response code = " + code);
104 }
105 }
106
107}