我正在开发一个Java程序,目的是将从几个网站收集的数据集成到输出中。我从网站获得了API信息,我可以在PHP中轻松运行它们,但是当我尝试使用Java时,只有一个网站存在一个奇怪的问题。我已将代码设置为遵循重定向,但如果我尝试访问https://www.foo.com,则会将我定向到127.0.0.1。无论我使用的协议还是包括/不包括www,它都会这样做。如果我拿出重定向代码,我会得到一个通用的"永久移动"页面生成。
以下是我使用
的代码public static void main(String args[]) throws IOException {
String URLString = "http://www.sickw.com/";
URL url = new URL(URLString);
URLConnection connection = url.openConnection();
System.out.println(url.toString()); //See what URL is being used
String redirect = connection.getHeaderField("Location");
while (redirect!=null)
{
System.out.println(redirect); //Follow the redirects
connection = new URL(redirect).openConnection();
redirect = connection.getHeaderField("Location");
}
System.out.println("new " + connection.getURL().toString()); //Print the final destination
InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
int temp = inputStreamReader.read();
while(temp!=-1)
{
System.out.print((char)temp);
temp = inputStreamReader.read();
}
}
答案 0 :(得分:0)
我在我的电脑中复制,粘贴和运行您的代码并获取正确的页面字符串(http://www.foo.com/) 我认为你的代码没有问题。所以请检查下面的事情。
答案 1 :(得分:0)
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
String url = "https://sickw.com";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
System.out.println("Requested URL -> " + url);
boolean redirect = false;
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
redirect = true;
}
}
System.out.println("Response Code -> " + status);
if (redirect) {
String newUrl = conn.getHeaderField("Location");
String cookies = conn.getHeaderField("Set-Cookie");
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
System.out.println("Redirect to URL -> " + newUrl);
}
StringBuilder html;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
String inputLine;
html = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
html.append("\n");
}
}
System.out.println("URL Content -> \n" + html.toString());
System.out.println("Completed");
} catch (IOException e) {
}
}
}
这可行,但使用" https://foo.com"