CHPP Development/Java/Login examples: Difference between revisions

From Hattrick
Jump to navigationJump to search
Filipx (talk | contribs)
No edit summary
Filipx (talk | contribs)
No edit summary
Line 1: Line 1:
You can choose:
1. [[http://wiki.hattrick.org/CHPP_Development/Java/JAXB Generating classes with JAXB and automatic parsing]] - more complicated at first, but gets easier as you go<br />
<br />
2. Old fashion way - complicated from start to finish :)<br />
<br />
<br />
<u><b>Step1</b>: Getting the recommended server</u>
<u><b>Step1</b>: Getting the recommended server</u>



Revision as of 17:22, 17 February 2006

You can choose:

1. [Generating classes with JAXB and automatic parsing] - more complicated at first, but gets easier as you go

2. Old fashion way - complicated from start to finish :)


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();
        cookie = connection.getHeaderField("Set-Cookie");
        cookie = cookie.substring(0, cookie.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;
 }