String fileURL="https://tools.keycdn.com/geo.json?host=192.168.6.9";
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
System.out.println(responseCode);
完整代码: 试试{
String url = "http://tools.keycdn.com/geo.json?host=192.168.6.9";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setInstanceFollowRedirects(true);
HttpURLConnection.setFollowRedirects(true);
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("Request URL ... " + url);
boolean redirect = false;
// normally, 3xx is redirect
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) {
// get redirect url from "location" header field
String newUrl =conn.getHeaderField("Location");
newUrl=newUrl.replace("https://", "http://");
// get the cookie if need, for login
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
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);
}
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
System.out.println("URL Content... \n" + html.toString());
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
} OUTPUT: 请求网址... http://tools.keycdn.com/geo.json?host=192.168.6.9 响应代码... 301 重定向到网址:http://tools.keycdn.com/geo.json?host=192.168.6.9 网址内容...... 301永久移动
我无法从这个链接获取json,它提供301响应代码,我试图从HTTP头部分获取重定向的URL,尽管如此,它返回相同的URL和相同的301响应代码,请给出java代码解决方案以获取来自此URL的JSON字符串。
答案 0 :(得分:1)
试试这个:
String fileURL="https://tools.keycdn.com/geo.json?host=192.168.6.9";
URL url;
try
{
final String USER_AGENT = "Mozilla/5.0";
url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// optional default is GET
httpConn.setRequestMethod("GET");
//add request header
httpConn.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = httpConn.getResponseCode();
System.out.println(responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(httpConn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
catch (Exception e)
{
e.printStackTrace();
}