【问题标题】:Not able to get Jsonstring from this link无法从此链接获取 Jsonstring
【发布时间】:2017-04-25 11:49:43
【问题描述】:
        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 标头部分获取重定向的 URL,尽管它返回相同的 URL 和相同的 301 响应代码,请提供 java 代码解决方案以获取此 URL 中的 JSON 字符串。

【问题讨论】:

  • 301 HTTP 代码表示此地址已永久移动。它还返回新地址的位置。您必须检查它以提出另一个请求或使用新地址更改您的代码
  • 请注意,您向 HTTPS 地址发出 HTTP 请求,因此您将被重定向到同一资源的 HTTPS 地址
  • 能否添加完整的 URL 响应?
  • @Yossi Vainshtein
  • @Yossi Vainshtein:{"status":"success","description":"数据成功接收。","data":{"geo":{"host":"192.168.6.9 ","ip":"192.168.6.9","route":null,"rdns":"192.168.6.9","asn":"","isp":"","abuse":null,"country_name ":null,"country_code":null,"region":null,"city":null,"postal_code":null,"continent_code":null,"latitude":null,"longitude":null,"dma_code": null,"area_code":null,"timezone":false,"datetime":"na"}}}

标签: java


【解决方案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();
    }

【讨论】:

  • OP 的请求已发送,但收到了 301,这样可以解决问题吗?有趣的是你强制用户代理到 Mozilla/5.0,我不明白为什么...... 你可能想解释你的解决方案
  • @AxelH 对我来说,上面给出的代码抛出 java.net.SocketException: Unexpected end of file from server。所以我添加了“用户代理”。它工作正常。希望这能解决他的问题。
  • 对我来说,它返回 java.net.ConnectException: Connection timed out: connect Excption
  • @Raghaven 对我来说它正在工作。获取响应代码为 200。希望您的网络存在问题。您可以通过浏览器访问该链接吗?
猜你喜欢
  • 2022-01-02
  • 1970-01-01
  • 2021-02-14
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多