Guest User

Untitled

a guest
Mar 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 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. } catch (IOException e) {
  25. try {
  26. if (con != null) con.disconnect();
  27. } catch (Exception e2) {
  28. }
  29. e.printStackTrace();
  30. }
  31.  
  32.  
  33. public String readInputStream(InputStream in) throws IOException, UnsupportedEncodingException {
  34. StringBuffer sb = new StringBuffer();
  35. String st = "";
  36.  
  37. BufferedReader br = new BufferedReader(new InputStreamReader(in, "Shift_JIS"));
  38. while((st = br.readLine()) != null) {
  39. sb.append(st);
  40. }
  41. try {
  42. in.close();
  43. }
  44. catch(Exception e) {
  45. e.printStackTrace();
  46. }
  47. return sb.toString();
  48. }
Add Comment
Please, Sign In to add comment