回答編集履歴

1

gzip解析方法を追記

2021/07/11 01:45

投稿

meex
meex

スコア63

test CHANGED
@@ -81,3 +81,157 @@
81
81
 
82
82
 
83
83
  を使うことにより解決できました。
84
+
85
+
86
+
87
+
88
+
89
+ ---
90
+
91
+
92
+
93
+ なお、KeepaのGZIPレスポンスを解析するときは「new GZIPInputStream(is,100);」
94
+
95
+ を使うとうまくいきました。
96
+
97
+
98
+
99
+ ```java
100
+
101
+ HttpURLConnection urlConn = null;
102
+
103
+ InputStream is = null;
104
+
105
+ GZIPInputStream in = null;
106
+
107
+ BufferedReader reader = null;
108
+
109
+
110
+
111
+ StringBuilder output = new StringBuilder();
112
+
113
+
114
+
115
+ try {
116
+
117
+ //接続するURLを指定する
118
+
119
+ URL url = new URL(strUrl);
120
+
121
+
122
+
123
+ //コネクションを取得する
124
+
125
+ urlConn = (HttpURLConnection) url.openConnection();
126
+
127
+ urlConn.setRequestMethod("GET");
128
+
129
+ urlConn.setRequestProperty("Accept-Encoding", "gzip");
130
+
131
+ urlConn.setRequestProperty("Accept", "gzip, deflate");
132
+
133
+ urlConn.setRequestProperty("Content-Encoding", "gzip");
134
+
135
+ urlConn.setRequestProperty("Connection", "Keep-Alive");
136
+
137
+ urlConn.setRequestProperty("Accept-Language", "jp");
138
+
139
+ urlConn.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; U; Intel Mac OS X; ja-JP-mac; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
140
+
141
+
142
+
143
+ urlConn.connect();
144
+
145
+
146
+
147
+ int status = urlConn.getResponseCode();
148
+
149
+
150
+
151
+ System.out.println("HTTPstatus: " + status);
152
+
153
+
154
+
155
+ if (status == HttpURLConnection.HTTP_OK) {
156
+
157
+
158
+
159
+ is = urlConn.getInputStream();
160
+
161
+
162
+
163
+ in = new GZIPInputStream(is,100);
164
+
165
+
166
+
167
+ reader = new BufferedReader(new InputStreamReader(in));
168
+
169
+
170
+
171
+ String line;
172
+
173
+
174
+
175
+ while ((line = reader.readLine()) != null) {
176
+
177
+ System.out.println(222);
178
+
179
+ output.append(line);
180
+
181
+ // JSONObject myjson = new JSONObject(line);
182
+
183
+ // JSONArray the_json_array = myjson.getJSONArray("profiles");
184
+
185
+
186
+
187
+ }
188
+
189
+
190
+
191
+ System.out.println(output.toString());
192
+
193
+ }
194
+
195
+
196
+
197
+ } catch (IOException e) {
198
+
199
+ e.printStackTrace();
200
+
201
+ } finally {
202
+
203
+ try {
204
+
205
+ if (reader != null) {
206
+
207
+ reader.close();
208
+
209
+ }
210
+
211
+ if (urlConn != null) {
212
+
213
+ urlConn.disconnect();
214
+
215
+ }
216
+
217
+ } catch (IOException e) {
218
+
219
+ e.printStackTrace();
220
+
221
+ }
222
+
223
+ }
224
+
225
+ return
226
+
227
+ //"OK"
228
+
229
+ output.toString()
230
+
231
+ ;
232
+
233
+ }
234
+
235
+ ```
236
+
237
+ ![イメージ説明](a405cdd35508f991e084a900968bdf7a.png)