Guest User

Untitled

a guest
Mar 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. try {
  2. // 接続用HttpURLConnectionオブジェクト作成
  3. HttpURLConnection con = null;
  4. // URLの作成
  5. URL urlSt = "https://cs.kintetsu-ls.co.jp/TR/TRGG0020/TRGG0020.aspx?ID=1234567890";
  6. url = new URL(urlSt);
  7. con = (HttpURLConnection) url.openConnection();
  8. // リダイレクトを自動で許可しない設定
  9. con.setInstanceFollowRedirects(false);
  10. // URL接続からデータを読み取る場合はtrue
  11. con.setDoInput(true);
  12. // URL接続にデータを書き込む場合はtrue
  13. con.setDoOutput(true);
  14. // 接続
  15. con.connect();
  16. // 本文の取得
  17. InputStream in = con.getInputStream();
  18. String readSt = readInputStream(in);
  19. //文字コードを指定して変換する
  20. readSt = new String(readSt.getBytes("Shift_JIS"));
  21.  
  22. System.out.println(readSt);//ここでブレークを張って、デバックエリアでreadStの中を覗いて取得文字をキャプチャ
  23.  
  24. //切断
  25. in.close();
  26. con.disconnect();
  27.  
  28. } catch (IOException e) {
  29. try {
  30. if (con != null) con.disconnect();
  31. } catch (Exception e2) {
  32. }
  33. e.printStackTrace();
  34. }
  35.  
  36.  
  37. public String readInputStream(InputStream in) throws IOException, UnsupportedEncodingException {
  38. StringBuffer sb = new StringBuffer();
  39. String st = "";
  40.  
  41. BufferedReader br = new BufferedReader(new InputStreamReader(in, "Shift_JIS"));
  42. while((st = br.readLine()) != null) {
  43. sb.append(st);
  44. }
  45. try {
  46. in.close();
  47. }
  48. catch(Exception e) {
  49. e.printStackTrace();
  50. }
  51. return sb.toString();
  52. }
Add Comment
Please, Sign In to add comment