Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Communicating With HBH - Java Code Bank


Communicating With HBH
This code provides a class that allows the user to easily and effectively communicate with HBH. Example applications would be for Timed Challenges, Real 2, Real 13, etc.
                import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author DopeBoiMag1k
 * @version 2.0.0
 * 
 * The HbhComm class abstracts away the use of the HttpURLConnection class
 * and allows the user to submit requests with or without post parameters
 * by simply supplying a valid Hellbound Hackers username and password.
 
 Example usage:
 public static void main(String[] args){
		try{
			hbh_url = \"http://www.hellboundhackers.org/challenges/timed/timed2/index.php\";
			HbhComm hbh = new HbhComm(\"username\", \"password\");
			
			BufferedReader in = hbh.send(hbh_url);
			
		    String value = methodToFindAnswer(in);
		    		    
		    hbh.addPostParameter(\"submit\", \"check\");
		    hbh.addPostParameter(\"ans\", \"\" + value);
		    
		    BufferedReader response = hbh.send(hbh_url + \"?check\");
		    while((source = response.readLine()) != null)
		    	System.out.println(source);
		    
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
 
 * 
 */
public class HbhComm {
	private ArrayList<PostParameter> post;
	private String user, pass, session, fusion;

	public static final String HBH_DEFAULT_REFERRER = \"http://www.hellboundhackers.org\";
	private static final String LOGIN_URL = \"http://www.hellboundhackers.org/index.php\";
	
	/**
	 * Default construct for the HbhComm class.  The only purpose is to
	 * initialize the underlying arraylist that contains the post parameters
	 * and to give the String in the class an intial value to avoid accidental
	 * NullPointerExceptions
	 * 
	 */
	public HbhComm(){
		post = new ArrayList<PostParameter>();
		user = pass = session = fusion = \"\";
	}
	
	/**
	 * Constructor for the HbhComm class.  The only use is to initialize
	 * the underlying arraylist that contains the post parameters and to
	 * give the Strings in the class an initial value
	 * 
	 */
	public HbhComm(String username, String password){
		post = new ArrayList<PostParameter>();
		user = username;
		pass = password;
				
		session = fusion = \"\";
	}
	
	/**
	 * Sends a post request to the HBH login page and stores the appropriate
	 * cookie parameters, so future requests can be made as an authenticated
	 * user
	 * 
	 * @throws IOException
	 */
	private void startSession() throws IOException{
		String uname = \"user_name=\" + URLEncoder.encode(user, \"UTF-8\");
		String pword = \"user_pass=\" + URLEncoder.encode(pass, \"UTF-8\");
		String login = \"login=\" + URLEncoder.encode(\"Login\", \"UTF-8\");
		
		HttpURLConnection connection = (HttpURLConnection) (new URL(LOGIN_URL)).openConnection();
		connection.setDoOutput(true);
		connection.setInstanceFollowRedirects(false);

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
		out.write(uname + \"&\" + pword + \"&\" + login);
		out.close();
		
		List<String> vals = connection.getHeaderFields().get(\"Set-Cookie\");
		for(int i = 0; i < vals.size(); i++){
			String cookie = vals.get(i).substring(0 , vals.get(i).indexOf(\';\') + 1);
			if(cookie.contains(\"fusion_user\"))
				fusion = cookie;
			
			if(cookie.contains(\"PHPSESSID\"))
				session = cookie;
		}
		
		connection.disconnect();
	}
	
	/**
	 * Sends the a request to the HBH server and stores the response in a
	 * BufferedReader object
	 * 
	 * @param url The url that is to be requested
	 * @return A BufferedReader object is returned that can be used to iterate through the HTML of the server response
	 * @throws IOException
	 */
	public BufferedReader send(String url) throws IOException{
		if(session.equals(\"\") || fusion.equals(\"\"))
			startSession();
		
		HttpURLConnection connection = (HttpURLConnection) (new URL(url)).openConnection();
		
	    connection.setRequestProperty(\"Cookie\", session + \" \" + fusion);
	    connection.setRequestProperty(\"referrer\", HBH_DEFAULT_REFERRER);
	    	    
	    connection.setDoOutput(true);
	    
	    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
	    
	    String params = \"\";
	    for(int i = 0; i < post.size(); i++){
	    	if(i == 0)
	    		params += post.get(i).encodeParameters();
	    	else
	    		params += \"&\" + post.get(i).encodeParameters();
	    }
	    
	    if(!params.equals(\"\"))
	    	out.write(params);
	    
	    out.close();
	    
	    return new BufferedReader(new InputStreamReader(connection.getInputStream()));
	}
	
	/**
	 * Sends the a request to the HBH server and stores the response in a
	 * BufferedReader object.  Use the method send(String url) to have the
	 * appropriate cookie parameters retrieved automatically
	 * 
	 * @param phpsessid The HBH user\'s PHPSESSID value, which can be copy & pasted from the cookie of the authenticated user.  It should be of the same form as in the cookie: \"PHPSESSID=phpsessid here;\".
	 * @param fusion_user The HBH user\'s fusion_user value, which can be copy & pasted from the cookie of the authenitcated user.  It should be of the same form as in the cookie:  \"fusion_user=fusion_user here;\" 
	 * @param hbh_url The url that is to be requested.
	 * @param referrer The referrer that is sent with the request.  HbhComm.HBH_DEFAULT_REFERRER can be specified to send HBH\'s default referrer.
	 * @return A BufferedReader object is returned that can be used to iterate through the HTML of the server response
	 * @throws IOException
	 */
	public BufferedReader send(String phpsessid, String fusion_user, String hbh_url, String referrer) throws IOException{
		HttpURLConnection connection = (HttpURLConnection) (new URL(hbh_url)).openConnection();
		
	    connection.setRequestProperty(\"Cookie\", phpsessid + \" \" + fusion_user);
	    connection.setRequestProperty(\"referrer\", referrer);
	    	    
	    connection.setDoOutput(true);
	    
	    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
	    
	    String params = \"\";
	    for(int i = 0; i < post.size(); i++){
	    	if(i == 0)
	    		params += post.get(i).encodeParameters();
	    	else
	    		params += \"&\" + post.get(i).encodeParameters();
	    }
	    
	    if(!params.equals(\"\"))
	    	out.write(params);
	    
	    out.close();
	    
	    return new BufferedReader(new InputStreamReader(connection.getInputStream()));
	}
	
	/**
	 * Adds the post parameter to the next request that is sent to the
	 * HBH server
	 * 
	 * @param paramName The name of the post parameter
	 * @param paramValue The value of the post parameter
	 */
	public void addPostParameter(String paramName, String paramValue){
		post.add(new PostParameter(paramName, paramValue));
	}
	
	/**
	 * Delets the post parameter from the next request that is sent to the
	 * HBH server
	 * 
	 * @param paramName The name of the post parameter to be removed
	 * @return Returns true upon successful removal and false if the post parameter can\'t be found
	 */
	public boolean removePostParameter(String paramName){
		for(int i = 0; i < post.size(); i++){
			if(post.get(i).getParameterName().equalsIgnoreCase(paramName)){
				post.remove(i);
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Removes all post parameters from the next request that is sent to
	 * the HBH server
	 * 
	 */
	public void removeAllPostParameters(){
		post = new ArrayList<PostParameter>();
	}
	
	/**
	 * Retrieves the fusion_user cookie parameter if it has not all ready been
	 * retrieved
	 * 
	 * @return The fusion_user cookie parameter
	 */
	public String getFusionUser(){
		if(fusion.equals(\"\")){
			try{
				startSession();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		
		return fusion;
	}
	
	/**
	 * Retrieves the PHPSESSID cookie parameter if it has not all ready been
	 * retrieved
	 * 
	 * @return The PHPSESSID cookie parameter
	 */
	public String getPHPSESSID(){
		if(session.equals(\"\")){
			try{
				startSession();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		
		return session;
	}
	
	/**
	 * 
	 * @author DopeBoiMag1k
	 *
	 * The inner class PostParamter is a data holding class that stores and
	 * returns an appropriately encoded String for submitting post parameters
	 * with an HTTP request
	 * 
	 */
	private class PostParameter {
		String name;
		String value;
		
		public PostParameter(String paramName, String paramValue){
			name = paramName;
			value = paramValue;
		}
		
		public String getParameterName(){
			return name;
		}
		
		public String encodeParameters() throws UnsupportedEncodingException{
			return name + \"=\" + URLEncoder.encode(value, \"UTF-8\");
		}
	}
}

            
Comments
Sorry but there are no comments to display