7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HTTP通信実装

Posted at

Httpで情報を取得するロジック

URL url = new URL("http://XXX.jp");

URLConnection uc = url.openConnection();
InputStream content = (InputStream)uc.getInputStream();
BufferedReader in =
new BufferedReader (new InputStreamReader (content));

String line;

while ((line = in.readLine()) != null) {
result.append(line);
}

in.close();

このままだとエラーが出た
Server returned HTTP response code: 401 for URL:
basic認証を使うようにソースを書き換えた

Basic認証の対応

URL url = new URL("http://ipros.jp");

String passwdstring = "id:password";
String encoding = new
sun.misc.BASE64Encoder().encode(passwdstring.getBytes());

URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", "Basic " + encoding);

InputStream content = (InputStream)uc.getInputStream();
BufferedReader in =
new BufferedReader (new InputStreamReader (content));

String line;

while ((line = in.readLine()) != null) {
result.append(line);
}

in.close();

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?