Chính sách chuyển hướng tự động được kiểm tra bất cứ khi nào nhận được mã phản hồi
public static String fetchRedirectURL[String url] throws IOException
{
HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[];
//System.out.println[ "orignal url: " + con.getURL[] ];
con.setInstanceFollowRedirects[false];
con.connect[];
InputStream is = con.getInputStream[];
if[con.getResponseCode[]==301]
return con.getHeaderField["Location"];
else return null;
}
public static void main[String[] args] throws MalformedURLException, IOException {
String url="//tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL[url];
System.out.println["FetchedURL is:"+fetchedUrl];
while[fetchedUrl!=null]
{ url=fetchedUrl;
System.out.println["The url is:"+url];
fetchedUrl=fetchRedirectURL[url];
}
System.out.println[url];
}
0. Nếu chuyển hướng không tự động xảy ra, thì phản hồi, chứa mã phản hồi public static String fetchRedirectURL[String url] throws IOException
{
HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[];
//System.out.println[ "orignal url: " + con.getURL[] ];
con.setInstanceFollowRedirects[false];
con.connect[];
InputStream is = con.getInputStream[];
if[con.getResponseCode[]==301]
return con.getHeaderField["Location"];
else return null;
}
public static void main[String[] args] throws MalformedURLException, IOException {
String url="//tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL[url];
System.out.println["FetchedURL is:"+fetchedUrl];
while[fetchedUrl!=null]
{ url=fetchedUrl;
System.out.println["The url is:"+url];
fetchedUrl=fetchRedirectURL[url];
}
System.out.println[url];
}
0, được trả về, nơi nó có thể được xử lý thủ côngChính sách
public static String fetchRedirectURL[String url] throws IOException
{
HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[];
//System.out.println[ "orignal url: " + con.getURL[] ];
con.setInstanceFollowRedirects[false];
con.connect[];
InputStream is = con.getInputStream[];
if[con.getResponseCode[]==301]
return con.getHeaderField["Location"];
else return null;
}
public static void main[String[] args] throws MalformedURLException, IOException {
String url="//tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL[url];
System.out.println["FetchedURL is:"+fetchedUrl];
while[fetchedUrl!=null]
{ url=fetchedUrl;
System.out.println["The url is:"+url];
fetchedUrl=fetchRedirectURL[url];
}
System.out.println[url];
}
2 được thiết lập thông qua Builder. phương thức followRedirects Tôi có một url chuyển hướng đến một url khác. Tôi muốn có thể nhận được URL được chuyển hướng cuối cùng. Mã của tôi
public class testURLConnection
{
public static void main[String[] args] throws MalformedURLException, IOException {
HttpURLConnection con =[HttpURLConnection] new URL[ "//tinyurl.com/KindleWireless" ].openConnection[];
System.out.println[ "orignal url: " + con.getURL[] ];
con.connect[];
System.out.println[ "connected url: " + con.getURL[] ];
InputStream is = con.getInputStream[];
System.out.println[ "redirected url: " + con.getURL[] ];
is.close[];
}
}
Nó luôn cung cấp url gốc trong khi redirectURL là. http. //www. amazon. com/kindle-wiress-reading-display-globally/dp/b003fsudm4/ref = amb_link_353259562_2?
Làm cách nào tôi có thể nhận được URL được chuyển hướng cuối cùng này
Đây là những gì tôi đã thử với vòng lặp cho đến khi chúng tôi nhận được chuyển hướng. Vẫn không tìm nạp url mong muốn
public static String fetchRedirectURL[String url] throws IOException
{
HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[];
//System.out.println[ "orignal url: " + con.getURL[] ];
con.setInstanceFollowRedirects[false];
con.connect[];
InputStream is = con.getInputStream[];
if[con.getResponseCode[]==301]
return con.getHeaderField["Location"];
else return null;
}
public static void main[String[] args] throws MalformedURLException, IOException {
String url="//tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL[url];
System.out.println["FetchedURL is:"+fetchedUrl];
while[fetchedUrl!=null]
{ url=fetchedUrl;
System.out.println["The url is:"+url];
fetchedUrl=fetchRedirectURL[url];
}
System.out.println[url];
}
Giải pháp tốt nhất
Hãy thử điều này, tôi sử dụng đệ quy để sử dụng cho nhiều URL chuyển hướng
public static String getFinalURL[String url] throws IOException {
HttpURLConnection con = [HttpURLConnection] new URL[url].openConnection[];
con.setInstanceFollowRedirects[false];
con.connect[];
con.getInputStream[];
if [con.getResponseCode[] == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode[] == HttpURLConnection.HTTP_MOVED_TEMP] {
String redirectUrl = con.getHeaderField["Location"];
return getFinalURL[redirectUrl];
}
return url;
}
và sử dụng
public static void main[String[] args] throws MalformedURLException, IOException {
String fetchedUrl = getFinalURL[""];
System.out.println["FetchedURL is:" + fetchedUrl];
}
Giải pháp liên quan
Java – Sự khác biệt giữa HashMap và Hashtable trong Java là gì
Có một số khác biệt giữa
public static String fetchRedirectURL[String url] throws IOException
{
HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[];
//System.out.println[ "orignal url: " + con.getURL[] ];
con.setInstanceFollowRedirects[false];
con.connect[];
InputStream is = con.getInputStream[];
if[con.getResponseCode[]==301]
return con.getHeaderField["Location"];
else return null;
}
public static void main[String[] args] throws MalformedURLException, IOException {
String url="//tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL[url];
System.out.println["FetchedURL is:"+fetchedUrl];
while[fetchedUrl!=null]
{ url=fetchedUrl;
System.out.println["The url is:"+url];
fetchedUrl=fetchRedirectURL[url];
}
System.out.println[url];
}
3 và public static String fetchRedirectURL[String url] throws IOException
{
HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[];
//System.out.println[ "orignal url: " + con.getURL[] ];
con.setInstanceFollowRedirects[false];
con.connect[];
InputStream is = con.getInputStream[];
if[con.getResponseCode[]==301]
return con.getHeaderField["Location"];
else return null;
}
public static void main[String[] args] throws MalformedURLException, IOException {
String url="//tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL[url];
System.out.println["FetchedURL is:"+fetchedUrl];
while[fetchedUrl!=null]
{ url=fetchedUrl;
System.out.println["The url is:"+url];
fetchedUrl=fetchRedirectURL[url];
}
System.out.println[url];
}
4 trong Java
4 được đồng bộ hóa, trong khipublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
3 thì không. Điều này làm chopublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
3 tốt hơn cho các ứng dụng không theo luồng, vì các Đối tượng không được đồng bộ hóa thường hoạt động tốt hơn các đối tượng được đồng bộ hóapublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
4 không cho phép khóa hoặc giá trị củapublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
0.public static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
3 cho phép một khóapublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
0 và bất kỳ số lượng giá trịpublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
0 nàopublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
Một trong các lớp con của HashMap là
4, vì vậy trong trường hợp bạn muốn thứ tự lặp lại có thể dự đoán được [thứ tự chèn theo mặc định], bạn có thể dễ dàng hoán đổipublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
3 lấypublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
4. Điều này sẽ không dễ dàng nếu bạn đang sử dụngpublic static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
4public static String fetchRedirectURL[String url] throws IOException { HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[]; //System.out.println[ "orignal url: " + con.getURL[] ]; con.setInstanceFollowRedirects[false]; con.connect[]; InputStream is = con.getInputStream[]; if[con.getResponseCode[]==301] return con.getHeaderField["Location"]; else return null; } public static void main[String[] args] throws MalformedURLException, IOException { String url="//tinyurl.com/KindleWireless"; String fetchedUrl=fetchRedirectURL[url]; System.out.println["FetchedURL is:"+fetchedUrl]; while[fetchedUrl!=null] { url=fetchedUrl; System.out.println["The url is:"+url]; fetchedUrl=fetchRedirectURL[url]; } System.out.println[url]; }
Vì đồng bộ hóa không phải là vấn đề đối với bạn, tôi khuyên bạn nên dùng
public static String fetchRedirectURL[String url] throws IOException
{
HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[];
//System.out.println[ "orignal url: " + con.getURL[] ];
con.setInstanceFollowRedirects[false];
con.connect[];
InputStream is = con.getInputStream[];
if[con.getResponseCode[]==301]
return con.getHeaderField["Location"];
else return null;
}
public static void main[String[] args] throws MalformedURLException, IOException {
String url="//tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL[url];
System.out.println["FetchedURL is:"+fetchedUrl];
while[fetchedUrl!=null]
{ url=fetchedUrl;
System.out.println["The url is:"+url];
fetchedUrl=fetchRedirectURL[url];
}
System.out.println[url];
}
3. Nếu đồng bộ hóa trở thành vấn đề, bạn cũng có thể xem public static String fetchRedirectURL[String url] throws IOException
{
HttpURLConnection con =[HttpURLConnection] new URL[ url ].openConnection[];
//System.out.println[ "orignal url: " + con.getURL[] ];
con.setInstanceFollowRedirects[false];
con.connect[];
InputStream is = con.getInputStream[];
if[con.getResponseCode[]==301]
return con.getHeaderField["Location"];
else return null;
}
public static void main[String[] args] throws MalformedURLException, IOException {
String url="//tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL[url];
System.out.println["FetchedURL is:"+fetchedUrl];
while[fetchedUrl!=null]
{ url=fetchedUrl;
System.out.println["The url is:"+url];
fetchedUrl=fetchRedirectURL[url];
}
System.out.println[url];
}
9