CHPP Development/Java/Login examples

From Hattrick

Connecting to the server


Step1: Getting the recommended server


public boolean getRecommendedServer() {
   try {
     URL url = new URL("http://www.hattrick.org/Common/menu.asp?outputType=XML");
     URLConnection connection = url.openConnection();
     BufferedReader br = new BufferedReader(
              new InputStreamReader(connection.getInputStream()));
     String line = "";
     while ((line = br.readLine()) != null) {
        //do something with the line
        //maybe write it into a file
     }
     //parse the file and get the recommended server
     //from the tags <RecommendedURL>www47.hattrick.org</RecommendedURL>
     //place it into a variabile named String recommendedURL, for example;
     connection.close();
     br.close();
  } catch (Exception e) {
     e.printStackTrace();
     return false;
  }
  return true;
}

Step2: Sign in


 public boolean signIn() {
    try {
        String userName = "myUsername";
        String securityCode = "myPass";
        String userAgent = "MyAppName/v1.0";
        String cookie = "";
        URL url = new URL(recommendedURL +
               "/common/default.asp?outputType=XML&actionType=login&loginType=CHPP&Loginname=" +
               userName + "&readonlypassword=" + securityCode);
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("User-Agent", userAgent);
        connection.connect();
        Map<String, List<String>> hm = huc.getHeaderFields();
        List<String> cookies = (List<String>)hm.get("Set-Cookie");
        for (String elem : cookies) {
            cookie += elem.substring(0, elem.indexOf(";")) + ";";
        }
        BufferedReader br = new BufferedReader(
             new InputStreamReader(connection.getInputStream()));
        String line = "";
        while ((line = br.readLine()) != null) {
            //do something with this
            //maybe check if login was successful
        }
        connection.close();
    } catch (Exception e) {
          e.printStackTrace();
          return false;
    }
    return true;
}

Step3: Download some files that you need

 public boolean downloadTeamDetails() {
    try {
       URL url = new URL(recommendedURL +
                  "/common/teamDetails.asp?outputType=XML&actionType=view");
       URLConnection connection = url.openConnection();
       connection.setRequestProperty("User-Agent", userAgent);
       connection.setRequestProperty("Cookie", cookie);
       BufferedReader br = new BufferedReader(
             new InputStreamReader(connection.getInputStream()));
       BufferedWriter bw = new BufferedWriter(
             new FileWriter("xml\\teamDetails.xml"));
       String line = "";
       while ((line = br.readLine()) != null) {
           bw.write(line + '\n');
       }
       bw.flush();
       br.close();
       bw.close();
       connection.close();
   } catch (Exception e) {
       e.printStackTrace();
       return false;
   }
   return true;
 }

Step4: Sign out

 public boolean signOut() {
    try {
        URL url = new URL(recURL + "/common/default.asp?outputType=XML&actionType=logout");
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("User-Agent", userAgent);
        connection.setRequestProperty("Cookie", cookie);
        connection.connect();
        //no need to open input stream, just connect
        connection.close();
    } catch (Exception e) {
          e.printStackTrace();
          return false;
    }
    return true;
 }




Parsing the XML files can be done the old fashion way, or in a simpler way, with the help of jaxb: [Generating classes with JAXB and automatic parsing] - it's more complicated at first, but gets easy as you go