質問編集履歴
2
URLの種別とAPI提供元サイトの情報を追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -151,3 +151,19 @@
|
|
151
151
|
どこか改善点がありましたらご指摘いただけませんでしょうか。
|
152
152
|
|
153
153
|
よろしくお願いします。
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
---
|
158
|
+
|
159
|
+
- 捕捉情報
|
160
|
+
|
161
|
+
APIのURLはhttpsでした。
|
162
|
+
|
163
|
+
また、ブラウザからnew URLの引数であるURLを叩くと、画像が表示されます。
|
164
|
+
|
165
|
+
※前回リクエスト時から30分経過するとログイン画面に飛ばされます(API提供元サイトのセッション有効時間が30分だと思われるため)。
|
166
|
+
|
167
|
+
以上、何かヒントになればと思い掲載させていただきました。
|
168
|
+
|
169
|
+
よろしくお願いします。
|
1
途中で投稿してしまったため更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,4 +4,150 @@
|
|
4
4
|
|
5
5
|
レスポンスコード200で正常に取得できているのですが、体感30分経過するとそのリクエストのレスポンスコードが401 Unautholizedになってしまいます。
|
6
6
|
|
7
|
+
リクエストの流れとして、CookieManagerを使用したコネクションの確立を最初に行ってからリクエストを行っています。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
以下コードになります。
|
12
|
+
|
13
|
+
```java
|
14
|
+
|
15
|
+
public HttpURLConnection getConnection() throws Exception {
|
16
|
+
|
17
|
+
HttpURLConnection con = null;
|
18
|
+
|
19
|
+
try {
|
20
|
+
|
21
|
+
CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
|
22
|
+
|
23
|
+
CookieHandler.setDefault(manager);
|
24
|
+
|
25
|
+
URL url = new URL("APIのURL");
|
26
|
+
|
27
|
+
con = (HttpURLConnection) url.openConnection();
|
28
|
+
|
29
|
+
con.setDoOutput(true);
|
30
|
+
|
31
|
+
con.setRequestMethod("POST");
|
32
|
+
|
33
|
+
con.setRequestProperty("Content-Type", "application/json");
|
34
|
+
|
35
|
+
con.setRequestProperty("Accept", "application/json");
|
36
|
+
|
37
|
+
String requestBody = String.format("{\"mail_address\": \"%s\", \"password\": \"%s\"}", "admin", "pass");
|
38
|
+
|
39
|
+
OutputStream os = con.getOutputStream();
|
40
|
+
|
41
|
+
os.write(requestBody.getBytes(Charset.forName("UTF-8")));
|
42
|
+
|
43
|
+
if (con.getResponseCode() == 200) {
|
44
|
+
|
45
|
+
StringBuilder responseBody = new StringBuilder();
|
46
|
+
|
47
|
+
InputStream is = con.getInputStream();
|
48
|
+
|
49
|
+
InputStreamReader isReader = new InputStreamReader(is, "UTF-8");
|
50
|
+
|
51
|
+
BufferedReader bufReader = new BufferedReader(isReader);
|
52
|
+
|
53
|
+
String line = null;
|
54
|
+
|
55
|
+
while((line = bufReader.readLine()) != null) {
|
56
|
+
|
57
|
+
responseBody.append(line);
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
bufReader.close();
|
62
|
+
|
63
|
+
isReader.close();
|
64
|
+
|
65
|
+
is.close();
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
System.out.println("レスポンスボディ: " + responseBody.toString());
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
} catch (Exception e) {
|
74
|
+
|
75
|
+
System.out.println("エラー");
|
76
|
+
|
77
|
+
con = null;
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
return con;
|
82
|
+
|
83
|
+
}
|
84
|
+
|
7
|
-
|
85
|
+
```
|
86
|
+
|
87
|
+
上記で取得したコネクションを使用し、下記コードによりリクエストを行っています。
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
```java
|
94
|
+
|
95
|
+
public InputStream getImageEntity(HttpURLConnection con) throws Exception {
|
96
|
+
|
97
|
+
InputStream is = null;
|
98
|
+
|
99
|
+
// URLの作成
|
100
|
+
|
101
|
+
URL url = new URL("APIのURL");
|
102
|
+
|
103
|
+
con = (HttpURLConnection) url.openConnection();
|
104
|
+
|
105
|
+
con.setRequestMethod("GET");
|
106
|
+
|
107
|
+
con.setDoOutput(true);
|
108
|
+
|
109
|
+
con.setInstanceFollowRedirects(false);
|
110
|
+
|
111
|
+
con.setRequestProperty("Accept", "image/jpeg");
|
112
|
+
|
113
|
+
con.connect();
|
114
|
+
|
115
|
+
int resCode = con.getResponseCode();
|
116
|
+
|
117
|
+
if (resCode == HttpURLConnection.HTTP_OK) {
|
118
|
+
|
119
|
+
is = con.getInputStream();
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
return is;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
捕捉として、上記InputStreamの戻り値がnullだった場合にはNO_IMAGEの画像をブラウザに返すようにしています。
|
132
|
+
|
133
|
+
なお、CookieHandler.getDefault()が存在する場合はコネクションの取得は省略しています。
|
134
|
+
|
135
|
+
```java
|
136
|
+
|
137
|
+
if (CookieHandler.getDefault() == null) {
|
138
|
+
|
139
|
+
con = getConnection();
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
```
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
リクエスト先のサーバはこちらからは閲覧できないのですが、私が知識不足なのもありこちらのコードが間違っているのかと思っています。
|
150
|
+
|
151
|
+
どこか改善点がありましたらご指摘いただけませんでしょうか。
|
152
|
+
|
153
|
+
よろしくお願いします。
|