Thanks for taking a look at my question 🙂
I’m making a program to launch the game “Ace of Spades” for me. The only way to play the game right now is to open up the game website in your browser, search for a good server, and then hope it hasn’t become full by the time you click on it. So I figured that making a launcher to organize these servers for me would be a fun and useful project.
However, I’ve run into a weird error that I’m not sure how to fix: “java.io.IOException: Server reutrned HTTP response code: 403 for URL: http://www.ace-spades.com/play/“.
My browser setup will load most websites just fine (including “https://google.com”), but for some reason the Ace of Spades website is turning it down! It’s not a typo, and the website isn’t down or anything (it loads just fine in Google Chrome), so I think it must be refusing access as a safety protocol to avoid DDoS attacks or something of the sort. So, if it works fine in Chrome, I think that getting my browser to emulate Chrome (or some other popular browser) in a certain regard might solve this problem. Or maybe I’m just doing something silly and stupid in my program (I’m a beginner with Java). Can you help me?
Here is my program:
//*****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();}
}
);
}
}
You can use an URLConnection and set the User-Agent:
Basically, you could subclass JEditorPane and override getStream(URL page) to add the User-Agent string.