0

私の質問を見てくれてありがとう:)

ゲーム「Ace of Spades」を起動するプログラムを作成しています。現時点でゲームをプレイする唯一の方法は、ブラウザでゲームの Web サイトを開き、適切なサーバーを検索して、クリックするまでに満杯にならないようにすることです。そのため、これらのサーバーを整理するためのランチャーを作成することは、楽しくて便利なプロジェクトになると考えました。

ただし、修正方法がわからないという奇妙なエラーが発生しました:「java.io.IOException: Server reutrned HTTP response code: 403 for URL: http://www.ace-spades.com/play /」。

私のブラウザの設定では、ほとんどのウェブサイト (「https://google.com」を含む) は問題なく読み込まれますが、何らかの理由で、エース オブ スペードのウェブサイトがそれを拒否しています! これはタイプミスではなく、ウェブサイトがダウンしていたり​​もしていません (Google Chrome では問題なく読み込まれます)。したがって、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

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ページ)をオーバーライドして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 に答える
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 に答える