Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try {
- // 接続用HttpURLConnectionオブジェクト作成
- HttpURLConnection con = null;
- // URLの作成
- URL urlSt = "https://cs.kintetsu-ls.co.jp/TR/TRGG0020/TRGG0020.aspx?ID=1234567890";
- url = new URL(urlSt);
- con = (HttpURLConnection) url.openConnection();
- // リダイレクトを自動で許可しない設定
- con.setInstanceFollowRedirects(false);
- // URL接続からデータを読み取る場合はtrue
- con.setDoInput(true);
- // URL接続にデータを書き込む場合はtrue
- con.setDoOutput(true);
- // 接続
- con.connect();
- // 本文の取得
- InputStream in = con.getInputStream();
- String readSt = readInputStream(in);
- //文字コードを指定して変換する
- readSt = new String(readSt.getBytes("Shift_JIS"));
- System.out.println(readSt);//ここでブレークを張って、デバックエリアでreadStの中を覗いて取得文字をキャプチャ
- //切断
- in.close();
- con.disconnect();
- } catch (IOException e) {
- try {
- if (con != null) con.disconnect();
- } catch (Exception e2) {
- }
- e.printStackTrace();
- }
- public String readInputStream(InputStream in) throws IOException, UnsupportedEncodingException {
- StringBuffer sb = new StringBuffer();
- String st = "";
- BufferedReader br = new BufferedReader(new InputStreamReader(in, "Shift_JIS"));
- while((st = br.readLine()) != null) {
- sb.append(st);
- }
- try {
- in.close();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- // バイト列をShift_JISとして解釈してUTF-16に変換
- BufferedReader br = new BufferedReader(new InputStreamReader(in, "Shift_JIS"));
- ...
- // UTF-16をShift_JISのバイト列に変換
- byte[] bytes = readSt.getBytes("Shift_JIS")
- // Shift_JISのバイト列を(デフォルトのUTF-8として解釈して)UTF-16に変換する
- readSt = new String(bytes);
Add Comment
Please, Sign In to add comment