这是去年年底一直想解决的问题(这么简单的问题,现在才解决,说出来有点丢人,唉。。。)。昨天终于知道怎么解决了,原来是就是用了HttpURLConnection类的getHeaderField("location");方法。然后就是简单而又繁琐的拼接URL了,下面是我的项目中自己写的一个方法,大家见笑了。。。
public static Entity_Page DownLoadContent(String str) {
// System.out.println(" str : " + str);
Entity_Page page = new Entity_Page();
String content = null;
URL url = null;
BufferedReader br = null;
HttpURLConnection conn = null;
InputStream is = null;
String encode = null;
boolean judge = false;
String title = "";
int responsecode;
StringBuffer sb = null;
try {
str = ToChinese(str);
url = new URL(str);
conn = (HttpURLConnection) url.openConnection();
// HttpURLConnection.setFollowRedirects(true);
conn.setFollowRedirects(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("User_Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
conn.connect();
responsecode = conn.getResponseCode();
if (responsecode == 200) {
is = conn.getInputStream(); //error
encode = GetEncode(url); //得到该页面的编码
br = new BufferedReader(new InputStreamReader(is, encode));
sb = new StringBuffer();
String line = "";
while ( (line = br.readLine()) != null) {
sb.append(line);
}
content = sb.toString();
page.encode = encode; //该网页的编码方式
page.content = content; //该网页的内容
page.title = title;
page.seed = str;
// page.judge = judge; //是否为完整的网页
}
else if (responsecode == 300 || responsecode == 301 ||
responsecode == 302 || responsecode == 304) {
String s = str.substring(7);
String u = null;
if (s.indexOf('/') != -1) {
u = "http://" + s.substring(0, s.indexOf('/')) +
conn.getHeaderField("location");
// System.out.println("11========== : " + u);
}
else {
u = "http://" + s + conn.getHeaderField("location");
// System.out.println("22========== : " + u);
}
URL uu = new URL(u); //u为最终重定向后的URL
conn = (HttpURLConnection) uu.openConnection();
conn.connect();
InputStream stream = conn.getInputStream();
encode = GetEncode(uu);
br = new BufferedReader(new InputStreamReader(stream, encode));
sb = new StringBuffer();
String line = "";
while ( (line = br.readLine()) != null) {
sb.append(line);
}
content = sb.toString();
page.encode = encode; //该网页的编码方式
page.content = content; //该网页的内容
page.title = title;
page.seed = u; //设置重定向后的URL
}
else {
System.err.println("Connection error : " + responsecode + " " +
url);
return null;
}
}
catch (Exception ex) {
// System.out.println("????????? " + url);
// ex.printStackTrace(); 需要处理
System.err.println("Connection timed out : " + url);
}
finally {
try {
if (sb != null) {
sb = null;
}
if (br != null) {
br.close();
br = null;
}
if (is != null) {
is.close();
is = null;
}
if (conn != null) {
conn.disconnect();
conn = null;
}
}
catch (IOException ex1) {
ex1.printStackTrace();
}
}
return page;
}
网上查资料时,都说用HttpClient好使,但我没试验成功。。。还得继续努力呀。。。