0

感谢您查看我的问题:)

我正在为我制作一个程序来启动游戏“黑桃 A”。现在玩游戏的唯一方法是在浏览器中打开游戏网站,搜索一个好的服务器,然后希望在您点击它时它还没有满员。所以我认为制作一个启动器来为我组织这些服务器将是一个有趣且有用的项目。

但是,我遇到了一个奇怪的错误,我不确定如何修复:“java.io.IOException:服务器重新发送 HTTP 响应代码:403 for URL:http ://www.ace-spades.com/play / "。

我的浏览器设置可以很好地加载大多数网站(包括“https://google.com”),但由于某种原因黑桃王牌网站拒绝了它!这不是一个错字,网站没有关闭或任何东西(它在谷歌浏览器中加载得很好),所以我认为它必须拒绝访问作为一种安全协议,以避免 DDoS 攻击或类似的东西。因此,如果它在 Chrome 中运行良好,我认为让我的浏览器在某些方面模拟 Chrome(或其他一些流行的浏览器)可能会解决这个问题。或者也许我只是在我的程序中做一些愚蠢和愚蠢的事情(我是 Java 的初学者)。你能帮助我吗?

这是我的程序:

//*****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();}
            }
        );
    }
}
4

2 回答 2

0

我不知道如何使用您正在使用的框架/库来做到这一点,但是将用户代理字符串设置为Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1可能会解决它。

于 2012-08-10T08:18:01.363 回答
0

您可以使用URLConnection并设置用户代理:

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 page) 以添加 User-Agent 字符串。

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();
    }

}
于 2012-08-10T08:38:29.223 回答