【问题标题】:I dont understand this http get我不明白这个http get
【发布时间】:2015-05-21 21:45:02
【问题描述】:

我想从https://my.playstation.com/logged-in/my-profile/获取数据。

我用 Firebug 分析了网站,得到了以下结果:

如您所见,有一个GET /playstation/psn/profile/psnloginbar?0.27654...

我尝试过:

String url = "/playstation/psn/profile/psnloginbar?0.3596450921613723&_=1432238143275";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);

int responseCode = con.getResponseCode();

但它不起作用。

【问题讨论】:

  • 解释一下但它不起作用
  • 我很确定您必须传递整个 URL。它怎么会知道域是 my.playstation.com?
  • 你的 URL String 无效,它应该(至少)有 https://my.playstation.com/ 前缀,或类似的

标签: java http get


【解决方案1】:

下面的修改代码是一个成功连接的例子。 请注意完整的 URL。 我认为您的下一个问题是您必须找到一种验证自己的方法,因为您的原始 URL (/playstation/psn/profile/psnloginbar?0.27654...) 位于表单登录屏幕后面。

String url = "https://www.playstation.com/en-us/";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", USER_AGENT);
     //This is the http response code :  
    int responseCode = con.getResponseCode();

    // To actually get the html body text, read the stream, 
    // below is just one of many ways to to this:

    BufferedReader in = new BufferedReader(
    new InputStreamReader(obj.openStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine); //<-- prints each line of html                  
    in.close();

【讨论】:

  • 好的,非常感谢,我想尝试登录,但在那之后,我怎样才能检索到我正在寻找的信息?因为它没有作为纯文本出现在 html 中,可能与 jquery 一起出现? pd:我是新手。
  • con.getResponseCode();只会给你http响应代码。 200 = OK , 301 表示重定向 ...等。您需要实际阅读 InputStream 以获取正文的 html 内容。将修改答案。
猜你喜欢
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 2016-06-23
  • 2015-06-09
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
相关资源
最近更新 更多