Debugging 当我访问此网站时,如何模拟流行的web浏览器?/为什么会出现403错误?
谢谢你看我的问题:) 我正在制作一个程序,为我推出“黑桃王牌”游戏。现在玩游戏的唯一方法是在浏览器中打开游戏网站,搜索一个好的服务器,然后希望在你点击它的时候它还没有满。所以我想,制作一个启动器来为我组织这些服务器将是一个有趣而有用的项目 然而,我遇到了一个奇怪的错误,我不知道如何修复它:“java.io.IOException:Server reutrned HTTP响应代码:403 for URL:” 我的浏览器设置将加载大多数网站(包括“https://google.com但是由于某种原因,黑桃王牌网站拒绝了它!这不是一个打字错误,网站也没有宕机(在Google Chrome中加载得很好),所以我认为它必须作为一种安全协议来拒绝访问,以避免DDoS攻击或类似的攻击。因此,如果它在Chrome中运行良好,我认为让我的浏览器在某些方面模仿Chrome(或其他流行浏览器)可能会解决这个问题。或者我只是在我的程序中做一些愚蠢的事情(我是Java的初学者)。你能帮我吗 这是我的节目:Debugging 当我访问此网站时,如何模拟流行的web浏览器?/为什么会出现403错误?,debugging,browser,emulation,Debugging,Browser,Emulation,谢谢你看我的问题:) 我正在制作一个程序,为我推出“黑桃王牌”游戏。现在玩游戏的唯一方法是在浏览器中打开游戏网站,搜索一个好的服务器,然后希望在你点击它的时候它还没有满。所以我想,制作一个启动器来为我组织这些服务器将是一个有趣而有用的项目 然而,我遇到了一个奇怪的错误,我不知道如何修复它:“java.io.IOException:Server reutrned HTTP响应代码:403 for URL:” 我的浏览器设置将加载大多数网站(包括“https://google.com但是由于某种原因
//*****ADD-ONS*****
package browser;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.net.*;
import java.io.*;
//*****PROGRAM*****
public class MainClass{
//Initialize general variables
private JFrame frame;
private JPanel panelTop;
private JEditorPane editor;
private JScrollPane scroll;
private JTextField field;
private JButton button;
private URL url;
private String windowTitle = "Ace of Spades Launcher";
private String homePage = "http://www.ace-spades.com/play/"; //"https://google.com";
private int screenWidth = 854;
private int screenHeight = 480;
//MainClass CONSTRUCTOR
public MainClass(){
//Initialize Components
initComponents();
//Set up frame
frame.setTitle(windowTitle);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(screenWidth,screenHeight);
frame.setLocationRelativeTo(null);
frame.add(BorderLayout.NORTH,panelTop); //Add JPanel to north of JFrame
panelTop.add(field); //Add TextField to JPanel
panelTop.add(button); //Add "Go" button to JPanel
frame.add(BorderLayout.CENTER,scroll); //Add scroll pane to JFrame
frame.setVisible(true);
}
//COMPONENT INITIALIZER
private void initComponents(){
frame = new JFrame(); //Create the JFrame
panelTop = new JPanel(); //Create the JPanel used to hold the text field and button
try{ //Set the URL
url = new URL(homePage);
}catch(MalformedURLException mue){
JOptionPane.showMessageDialog(null,mue);}
try{ //Create the JEditorPane
editor = new JEditorPane(url);
editor.setEditable(false); //Set the editor pane to false
}catch(IOException ioe){
JOptionPane.showMessageDialog(null,ioe);}
scroll = new JScrollPane( //Create the scroll pane and add the JEditorPane to it
editor,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
);
field = new JTextField(); //Create the JTextField
/**NOTE: We're not doing this on the event dispatch thread, so we need to use SwingUtilities */
SwingUtilities.invokeLater( //Set the JTextField text to the URL
new Runnable(){
public void run(){
field.setText(url.toString());
}
}
);
button = new JButton("Go"); //Create the button for changing pages.
button.addActionListener( //Add action listener to the button
new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
editor.setPage(field.getText());
}catch(IOException ioe){
JOptionPane.showMessageDialog(null,ioe);}
}
}
);
editor.addHyperlinkListener( //Enable hyperlink clicking
new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent e){
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
try{
editor.setPage(e.getURL());
}catch(IOException ioe){
JOptionPane.showMessageDialog(null,ioe);}
}
}
}
);
}
//MAIN PROGRAM EXECUTER
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
new MainClass();}
}
);
}
}
我不知道如何使用您正在使用的框架/库,但将用户代理字符串设置为
Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.1(KHTML,如Gecko)Chrome/22.0.1207.1 Safari/537.1
可能会解决此问题。您可以使用
URL server = new URL("http://www.ace-spades.com/play");
URLConnection connection = server.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2");
基本上,您可以将JEditorPane子类化并覆盖getStream(URL页面)以添加用户代理字符串
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JEditorPane;
public class UserAgentEditorPane extends JEditorPane {
private static final long serialVersionUID = 1L;
private String userAgent;
public UserAgentEditorPane(URL url, String userAgent) throws IOException {
super(url);
this.userAgent = userAgent;
}
@Override
protected InputStream getStream(URL page) throws IOException {
URLConnection conn = page.openConnection();
conn.setRequestProperty("User-Agent", userAgent);
setContentType(conn.getContentType());
return conn.getInputStream();
}
}
我想,你实际上可以使用浏览器本身——创建一两个用户脚本来执行手动点击。@Christopher Creutzig我考虑过这一点,但我真的不想依赖其他类似的程序。我感兴趣的是浏览部分,而不是点击。我需要获取所有服务器的数据,并将其呈现给用户,以便用户能够做出选择。您可以在用户脚本中获取服务器的所有数据,并显示您想要的任何内容。但是,当然,如果出于某种原因,您发现依赖浏览器比仅仅依赖编译器和JRE糟糕得多,那么这不是一个选项。我仍然相信这会更容易,并且对服务器端的更改更具鲁棒性。我刚刚尝试了你的建议,但我得到了完全相同的错误:/I用你的建议替换了我的“//设置URL”部分(尽管我不得不用try&catch围绕一些代码)。我把你的建议执行错了吗?(我是一个初学者,我只是刚刚进入浏览器,所以我有点不知所措。)我运行的是Windows,而不是Mac操作系统。我是否需要更改连接请求属性字符串?请看我所做的编辑。UserAgentEditorPane应该可以做到这一点。谷歌有点尝试不同的用户代理,例如
Mozilla/5.0(Windows;U;Windows NT 5.1;de;rv:1.9.2.3)Gecko/20100401 Firefox/3.6.3(FM场景4.6.1)
适合我。