gpt4 book ai didi

java - 无法从此链接获取 Jsonstring

转载 作者:行者123 更新时间:2023-11-30 06:46:25 27 4
gpt4 key购买 nike

        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();
}

}输出:请求网址...http://tools.keycdn.com/geo.json?host=192.168.6.9响应代码... 301重定向至网址:http://tools.keycdn.com/geo.json?host=192.168.6.9网址内容...301 永久移动

301 永久移动


nginx完成

我无法从此链接获取json,它给出了301响应代码,我尝试从HTTP header 部分获取重定向的URL,尽管如此,它返回相同的URL和相同的301响应代码,请提供java代码解决方案以获取来自此 URL 的 JSON 字符串。

最佳答案

试试这个:

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();
}

关于java - 无法从此链接获取 Jsonstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609872/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com