|
1 | 1 | package dev.felnull.fnjl.util; |
2 | 2 |
|
3 | 3 | import dev.felnull.fnjl.FelNullJavaLibrary; |
| 4 | +import dev.felnull.fnjl.tuple.FNPair; |
4 | 5 |
|
5 | | -import java.io.BufferedReader; |
6 | | -import java.io.IOException; |
7 | | -import java.io.InputStream; |
8 | | -import java.io.InputStreamReader; |
| 6 | +import java.io.*; |
9 | 7 | import java.net.HttpURLConnection; |
10 | 8 | import java.net.URL; |
11 | 9 | import java.nio.charset.StandardCharsets; |
@@ -113,4 +111,70 @@ public static CompletableFuture<Void> getResponseAsync(URL url, Consumer<String> |
113 | 111 | stringConsumer.accept(str); |
114 | 112 | }); |
115 | 113 | } |
| 114 | + |
| 115 | + /** |
| 116 | + * POSTでテキストを送り返ってきた文字列とステータスコードを取得 |
| 117 | + * |
| 118 | + * @param url URL |
| 119 | + * @param body テキスト |
| 120 | + * @param language 言語 |
| 121 | + * @param contentType type |
| 122 | + * @param responseConsumer 返答とステータスコードのペア |
| 123 | + * @return 返答とステータスコードのペア |
| 124 | + */ |
| 125 | + public static CompletableFuture<Void> getResponseByPOSTAsync(URL url, String body, String language, String contentType, Consumer<FNPair<String, Integer>> responseConsumer) { |
| 126 | + return CompletableFuture.runAsync(() -> { |
| 127 | + FNPair<String, Integer> ret = null; |
| 128 | + try { |
| 129 | + ret = getResponseByPOST(url, body, language, contentType); |
| 130 | + } catch (IOException e) { |
| 131 | + e.printStackTrace(); |
| 132 | + } |
| 133 | + responseConsumer.accept(ret); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * POSTでテキストを送り返ってきた文字列とステータスコードを取得 |
| 139 | + * |
| 140 | + * @param url URL |
| 141 | + * @param body テキスト |
| 142 | + * @param language 言語 |
| 143 | + * @param contentType type |
| 144 | + * @return 返答とステータスコードのペア |
| 145 | + * @throws IOException 失敗 |
| 146 | + */ |
| 147 | + public static FNPair<String, Integer> getResponseByPOST(URL url, String body, String language, String contentType) throws IOException { |
| 148 | + HttpURLConnection con = getConnection(url); |
| 149 | + con.setDoOutput(true); |
| 150 | + con.setRequestMethod("POST"); |
| 151 | + con.setRequestProperty("Accept-Language", language); |
| 152 | + con.setRequestProperty("Content-Type", String.format("%s; charset=utf-8", contentType)); |
| 153 | + con.setRequestProperty("Content-Length", String.valueOf(body.getBytes(StandardCharsets.UTF_8).length)); |
| 154 | + |
| 155 | + OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream()); |
| 156 | + out.write(body); |
| 157 | + out.flush(); |
| 158 | + con.connect(); |
| 159 | + int sts = con.getResponseCode(); |
| 160 | + StringBuilder sb = new StringBuilder(); |
| 161 | + |
| 162 | + InputStream in = con.getInputStream(); |
| 163 | + String encoding = con.getContentEncoding(); |
| 164 | + if (null == encoding) { |
| 165 | + encoding = "UTF-8"; |
| 166 | + } |
| 167 | + InputStreamReader inReader = new InputStreamReader(in, encoding); |
| 168 | + BufferedReader bufReader = new BufferedReader(inReader); |
| 169 | + |
| 170 | + String line; |
| 171 | + while ((line = bufReader.readLine()) != null) { |
| 172 | + sb.append(line); |
| 173 | + } |
| 174 | + bufReader.close(); |
| 175 | + inReader.close(); |
| 176 | + in.close(); |
| 177 | + return FNPair.of(sb.toString(), sts); |
| 178 | + } |
| 179 | + |
116 | 180 | } |
0 commit comments