CHPP Development/Java/Login examples: Difference between revisions
From Hattrick
Jump to navigationJump to search
No edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
public boolean getRecommendedServer() { | public boolean getRecommendedServer() { | ||
try { | |||
URL url = new URL("http://www.hattrick.org/Common/menu.asp?outputType=XML"); | URL url = new URL("http://www.hattrick.org/Common/menu.asp?outputType=XML"); | ||
URLConnection connection = url.openConnection(); | URLConnection connection = url.openConnection(); | ||
| Line 13: | Line 13: | ||
//maybe write it into a file | //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) { | } catch (Exception e) { | ||
e.printStackTrace(); | e.printStackTrace(); | ||
| Line 19: | Line 24: | ||
return true; | return true; | ||
} | } | ||
<u><b>Step2</b>: Sign in</u> | |||
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; | |||
} | |||
<u><b>Step3</b>: Download some files that you need</u> | |||
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; | |||
} | |||
<u><b>Step4</b>: Sign out</u> | |||
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; | |||
} | |||
Revision as of 16:39, 17 February 2006
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;
}