Re: [問題] 使用URL呼叫網址,但跑出來的頁面結果ꐠ…
※ 引述《hellon (Jane)》之銘言:
: 各位大大,請幫我看看到底是什麼原因:
: URL url = new URL(strUrl);
: conn = (HttpURLConnection) url.openConnection();
: conn.setRequestMethod("GET");
: BufferedReader br = new BufferedReader(new
: InputStreamReader(conn.getInputStream(), "UTF-8"));
: String line = "";
: while ((line = br.readLine()) != null) {
: outStr = outStr + line;
: }
: 假設 StrUrl = tw.aaa.com/bb.aspx
: 但跑出來的網頁畫面竟然是www.aaa.com/bb.aspx
: 這到底是什麼原因呢?
: 跑au.aaa.com/bb.aspx,跑出來的畫面是正確的。
: 但就是跑tw.aaa.com/bb.aspx會跳到www.aaa.com/bb.aspx
: ps.先前有做個測試:conn.getURL().getHost() ------跑出來的結果是tw.aaa.com
: 請幫幫忙!!
你的描述我看不是很懂~
如果 request:/bb.aspx 送到 tw.aaa.com,而 server 要求 redierct 到
www.aaa.com/bb.aspx,但你不想要 redirect 後網頁(response),可以設定
HttpURLConnection 不要 follow redirect 要求。
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
http://tinyurl.com/3vdh2fh
http://tinyurl.com/3o2fz4w
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.173.130.223
→
04/06 01:57, , 1F
04/06 01:57, 1F