简体   繁体   English

Java中出现意外的URL请求结果

[英]Unexpected URL request result in Java

There is a site http://l2on.net/ with dropdown list: 有一个带有下拉列表的网站http://l2on.net/

<select size="1" onchange="window.location.href = '/?c=market&amp;a=search&amp;q=&amp;type=0&amp;setworld=' + this.value;">
<option selected value="43">Cadmus</option>
<option value="44">Athebaldt</option>
<option value="45">Blackbird</option>
<option value="46">Ramsheart</option>
<option value="47">Esthus</option>
<option value="49">Lancer</option>
<option value="13">Airin</option>
<option value="5">Erica</option>
<option value="27">Elcardia</option>
</select>

My guess is that it is possible to select the server I need with "GET" query, just like it is in the html code. 我的猜测是可以通过“ GET”查询选择我需要的服务器,就像在html代码中一样。

<select size="1" onchange="window.location.href = '/?c=market&amp;a=search&amp;q=&amp;type=0&amp;setworld=' + this.value;">

After suggestions here code looks like: 提出建议后,代码如下所示:

    l2on = new URL("http://l2on.net/?setworld=BlackBird");
    l2onConn = l2on.openConnection();
    l2onConn.setRequestProperty("Accept-Charset", "UTF-8");
    l2onConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
    l2onConn.connect();
    br = new BufferedReader(new InputStreamReader(l2onConn.getInputStream()));
    String line = null;
    while ((line = br.readLine()) != null) {
        content += line + System.lineSeparator();
    }
    System.out.println(content.contains("<option selected value=\"43\">Cadmus</option>"));

Also I tried UrlEncoder.encode() to encode query properly. 我也尝试了UrlEncoder.encode()来正确编码查询。 And passing as setworld parameter id of server (which I have found in html page source code), like http://l2on.net/?setworld=45 . 并作为服务器的setworld参数id传递(我在html页面源代码中找到),例如http://l2on.net/?setworld=45

All this things work perfectly from browser and don't work from Java( I know that because in accepted page after all I can find 所有这些东西都可以在浏览器中完美运行,而不能在Java中运行(我知道,因为毕竟在接受的页面中我可以找到

<option selected value="43">Cadmus</option>

which shows that the server is not changed in any way, and info for default one is shown, and want to retrieve somehow information for all possible servers. 这表明服务器没有任何变化,并且显示了默认服务器的信息,并且希望以某种方式检索所有可能服务器的信息。

Sorry, I don't know much about html, so probably I'm doing something wrong, 抱歉,我对html不太了解,所以可能我做错了什么,

Help please. 请帮助。

This may be a wild guess. 这可能是一个疯狂的猜测。 Could it be possible that the reason it works in your browser is that, in your browser, you're already authenticated to the website? 它可能在您的浏览器中起作用的原因可能是,在您的浏览器中,您已经通过了网站的身份验证?

My browser isn't authenticated to this website. 我的浏览器未通过此网站的身份验证。 When I try this URL: 当我尝试以下网址时:

http://l2on.net/?setworld=Atlant

I'm getting redirected to the home page: http://l2on.net . 我将重定向到主页: http://l2on.net : http://l2on.net

Which is exactly what happens to your Java program. 这正是Java程序会发生的情况。

Perhaps another wild guess, but this helped me recently when connecting to certain sites. 也许是另一个疯狂的猜测,但这最近在连接到某些站点时对我有所帮助。

Some sites block certain user agents, and by default, java is using a user-agent that makes you look like a bot. 有些站点会阻止某些用户代理,默认情况下,java使用的用户代理使您看起来像个机器人。 It will be something like "Java/1.6.0_14" with the latter part depending on the java version. 它将类似于“ Java / 1.6.0_14”,后一部分取决于Java版本。

Have you tried setting another user agent? 您是否尝试过设置其他用户代理? You can do it like this: 您可以这样做:

l2onConn.setRequestProperty("User-Agent", "Agent_you_want");

I'd suggest trying what your browser is using to start. 我建议尝试使用您的浏览器启动。 You can get that here . 你可以在这里得到。

If that works, then using something to identify yourself would probably be the right thing to do. 如果这行得通,那么使用某些东西来标识自己可能是正确的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM