gpt4 book ai didi

java - 无法从 java 代码读取 URL

转载 作者:行者123 更新时间:2023-12-01 13:16:23 25 4
gpt4 key购买 nike

我非常渴望得到这个 URL 的内容.

尝试从 Web 浏览器访问此页面时不需要身份验证,但当我尝试从 Web 应用程序获取内容时,我会收到 sso 文件作为响应。我使用的代码如下:

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://search.lib.monash.edu/primo_library/libweb/action/search.do?dscnt=1&frbg=&tab=default_tab&srt=rank&ct=search&mode=Basic&dum=true&tb=&indx=1&vl%28freeText0%29=java&fn=search&vid=MON");
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity responseEntity = httpResponse.getEntity();


BufferedReader in = new BufferedReader(
new InputStreamReader(responseEntity.getContent()));
String inputLine;
StringBuffer response = new StringBuffer();


while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

System.out.println(response.toString());

我收到的 sso 文件作为响应如下:

<!-- filename: sso --> <html> <head> <title>Login </title> <!-- START filename: meta-tags.pds --> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">  <META HTTP-EQUIV="Pragma" CONTENT="no-cache">  <META HTTP-EQUIV="Expires" CONTENT="Sun, 06 Nov 1994 08:49:37 GMT">  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"> <!-- END   filename: meta-tags.pds --> <link rel="stylesheet" href="http://monash-dc05.hosted.exlibrisgroup.com:8991/PDSMExlibris.css" TYPE="text/css"> </head> <body onload = "location = '/goto/http://search.lib.monash.edu:80/primo_library/libweb/action/login.do?afterPDS=true&vid=MON&vid=MON&dscnt=2&targetURL=http%3A%2F%2Fsearch.lib.monash.edu%2Fprimo_library%2Flibweb%2Faction%2Fsearch.do%3Fdscnt%3D0&frbg=&tab=default%5Ftab&dstmp=1394940513823&srt=rank&ct=search&mode=Basic&dum=true&indx=1&tb=&vl%28freeText0%29=java&fn=search&pds_handle=GUEST';"> <noscript> <div id="header">      <div>         <img src="http://monash-dc05.hosted.exlibrisgroup.com:8991//exlibris/primo/p4_1/pds/html_form/icon/exlibrislogo.jpg" alt="Exlibris Logo"><p>&nbsp;</p>     </div> </div> <div id="connect">  <a href="/goto/http://search.lib.monash.edu:80/primo_library/libweb/action/login.do?afterPDS=true&vid=MON&vid=MON&dscnt=2&targetURL=http%3A%2F%2Fsearch.lib.monash.edu%2Fprimo_library%2Flibweb%2Faction%2Fsearch.do%3Fdscnt%3D0&frbg=&tab=default%5Ftab&dstmp=1394940513823&srt=rank&ct=search&mode=Basic&dum=true&indx=1&tb=&vl%28freeText0%29=java&fn=search&pds_handle=GUEST">Return from Check SSO </a></noscript> </div> </body> </html></body></html>

请帮忙。

最佳答案

这不是因为任何身份验证问题。

返回的页面有一个与body关联的onload事件。由于这个原因,当您在浏览器客户端中打开引用的 URL 时,

  1. 它首先接收您在 response 中的响应 html字符串。
  2. 然后它会尝试渲染并显示它。
  3. 但是,与此同时,onload 事件会触发并加载 URL:由 location='/goto/..... 定义。
  4. 并且,在显示当前页面之前,接收新页面并显示在浏览器上。

从您收到的回复中,请注意以下内容:

<body onload = "location = '/goto/http://search.lib.monash.edu:80/primo_library/libweb/action/login.do?afterPDS=true&vid=MON&vid=MON&dscnt=2&targetURL=http%3A%2F%2Fsearch.lib.monash.edu%2Fprimo_library%2Flibweb%2Faction%2Fsearch.do%3Fdscnt%3D0&frbg=&tab=default%5Ftab&dstmp=1394940513823&srt=rank&ct=search&mode=Basic&dum=true&indx=1&tb=&vl%28freeText0%29=java&fn=search&pds_handle=GUEST';">

在JAVA代码中,您只是从您指定的URL中读取内容。
并且您不会将其传递给任何内容解析器来渲染和显示。除非如此,否则它将被视为静态文本。

因此,与在网络浏览器中看到的相比,您在 JAVA 代码中看不到响应。

其他建议:
当您读取一行并将其附加到缓冲区时,最好还附加一个 CRLF 到它。

更改:

    response.append(inputLine);

致:

    response.append( inputLine ).append( "\r\n" );

它使响应文本多行且更具可读性。

关于java - 无法从 java 代码读取 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22432667/

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