Hejsa... :)
Herunder er der en masse kode... det er mit forsøg på at logge ind på en
side (
http://www.hattrick.org/) følgende burde efter min mening poste til
siden og få en login session cookie eller lignende tilbage i hovedet igen...
men det sker bare ikke... håber der er nogen der kender lidt til det library
jeg benytter:
http://jakarta.apache.org/commons/httpclient/
jeg ved ikke om det er min benyttelse af libraryts post classses og methods
der er forkert... for de oplysninger er de samme som den form på login siden
angiver.... håber der er nogen der kan hjælpe mig... :-/
// Daniel Jensen
(Følgende kode i main burde udskrive en forkert login da det user/pass ikke
er korrekt men den gør ingenting overhovedet :-/...)
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import java.io.IOException;
public class GetPage {
private static String url = /*"
http://ip.ter.dk/";*/
"
http://www.hattrick.org/common/default.asp";
private static String startMenuUrl =
"
http://www.hattrick.org/Common/menu.asp";
private static String loginUrl = /*"
http://ip.ter.dk/";*/
"
http://195.149.159.155/Common/default.asp";
private static String checkLogin =
"menu.asp?timeOut=&loginError=&showMenu=login";
private static Cookie cookies[];
private static boolean haveCookie = false;
private static HttpClient client = new HttpClient();
public static void login (String username, String password) {
System.out.println("Udskrevet i starten af login()");
HttpState initialState = new HttpState();
initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);
System.out.println("Lige før if sætningen: if(!haveCookie)");
if(!haveCookie) {
String headerValueAccept = "image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword,
application/x-shockwave-flash, */*";
GetMethod get = new GetMethod(url);
System.out.println("Dette er Hvis vi ingen Session Cookie
havde");
get.setFollowRedirects(true);
get.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.1)");
get.setRequestHeader("Accept", headerValueAccept);
get.setRequestHeader("Accept-Encoding", "gzip, deflate");
get.setRequestHeader("Accept-Language", "da");
get.setRequestHeader("Connection", "Keep-Alive");
Header[] getRequestHeaders = get.getRequestHeaders();
System.out.println("Request headers i første sidehentning:");
for(int i = 0; i < getRequestHeaders.length; i++) {
System.out.println(i + ": " +
getRequestHeaders[i].toExternalForm());
}
client.setConnectionTimeout(5000);
client.setState(initialState);
try {
client.executeMethod(get);
} catch (IOException e) {
System.out.println("Der skete en fejl i hentningen af
default.asp: " + e.toString());
System.out.println();
e.printStackTrace();
}
Header[] getHeader = get.getResponseHeaders();
System.out.println("Response headers i første sidehentning:");
for(int i = 0; i < getHeader.length; i++) {
System.out.println(i + ": " +
getHeader[i].toExternalForm());
}
cookies = client.getState().getCookies();
System.out.println("Cookies: ");
for (int i = 0; i < cookies.length; i++) {
System.out.println(" - " + cookies[i].toExternalForm());
}
System.out.println();
System.out.println("Indholdet af Siden: ");
System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
if (cookies.length>0) {
haveCookie = true;
} else {
System.out.println("Fik ingen Cookie");
haveCookie = false;
}
}
PostMethod post = new PostMethod(loginUrl);
if(haveCookie) {
NameValuePair[] submitBody = {
new NameValuePair("loginname", username),
new NameValuePair("password", password)
};
post.setRequestBody(submitBody);
post.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.1)");
post.setFollowRedirects(true);
client.setConnectionTimeout(30000);
client.setState(initialState);
Header[] postMethodHeader = post.getRequestHeaders();
System.out.println("Post headers vi sender:");
for(int i = 0; i < postMethodHeader.length; i++) {
System.out.println(i + ": " +
postMethodHeader[i].toExternalForm());
}
try {
client.executeMethod(post);
} catch(IOException e) {
e.printStackTrace();
}
Header[] loginInputHeader = post.getResponseHeaders();
System.out.println("LoginHeaders fra Post query: ");
for(int i = 0; i < loginInputHeader.length; i++) {
System.out.println(i + ": " +
loginInputHeader[i].toExternalForm());
}
cookies = client.getState().getCookies();
for (int i = 0; i < cookies.length; i++) {
System.out.println(" - " + cookies[i].toExternalForm());
}
System.out.println("Indholdet af siden: ");
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
if(cookies.length>1) {
haveCookie = true;
} else {
System.out.println("Havde ikke mere end 1 Cookie");
haveCookie = false;
}
}
PostMethod postMethod = new PostMethod(loginUrl);
if(haveCookie) {
NameValuePair[] submitBody = {
new NameValuePair("loginname", username),
new NameValuePair("password", password)
};
postMethod.setRequestBody(submitBody);
postMethod.setRequestHeader("User-Agent", "Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.1)");
postMethod.setFollowRedirects(true);
client.setConnectionTimeout(30000);
client.setState(initialState);
Header[] postHeader = postMethod.getRequestHeaders();
System.out.println("Post headers vi sender:");
for(int i = 0; i < postHeader.length; i++) {
System.out.println(i + ": " +
postHeader[i].toExternalForm());
}
try {
client.executeMethod(postMethod);
} catch(IOException e) {
e.printStackTrace();
}
Header[] loginInputHeader = postMethod.getResponseHeaders();
System.out.println("LoginHeaders fra første login: ");
for(int i = 0; i < loginInputHeader.length; i++) {
System.out.println(i + ": " +
loginInputHeader[i].toExternalForm());
}
cookies = client.getState().getCookies();
for (int i = 0; i < cookies.length; i++) {
System.out.println(" - " + cookies[i].toExternalForm());
}
System.out.println(postMethod.getResponseBodyAsString());
postMethod.releaseConnection();
if(cookies.length>1) {
haveCookie = true;
} else {
System.out.println("Havde ikke mere end 1 Cookie");
haveCookie = false;
}
}
}
public static void get(String url) {
if(cookies.length>1) {
System.out.println("Udskrevet i starten af get(String url)");
HttpState initialState = new HttpState();
initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);
System.out.println("Lige før if sætningen: if(haveCookie)");
if(haveCookie) {
String headerValueAccept = "image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword,
application/x-shockwave-flash, */*";
GetMethod get = new GetMethod(url);
System.out.println("Dette er hvis vi havde en cookie");
get.setFollowRedirects(true);
get.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.1)");
get.setRequestHeader("Accept", headerValueAccept);
get.setRequestHeader("Accept-Encoding", "gzip, deflate");
get.setRequestHeader("Accept-Language", "da");
get.setRequestHeader("Connection", "Keep-Alive");
Header[] getRequestHeaders = get.getRequestHeaders();
System.out.println("Request headers i method get() sidehentning:
");
for(int i = 0; i < getRequestHeaders.length; i++) {
System.out.println(i + ": " +
getRequestHeaders[i].toExternalForm());
}
client.setConnectionTimeout(5000);
client.setState(initialState);
try {
client.executeMethod(get);
} catch (IOException e) {
System.out.println("Der skete en fejl i hentningen af
default.asp: " + e.toString());
System.out.println();
e.printStackTrace();
}
Header[] getHeader = get.getResponseHeaders();
System.out.println("Response headers i method get()
sidehentning:");
for(int i = 0; i < getHeader.length; i++) {
System.out.println(i + ": " +
getHeader[i].toExternalForm());
}
cookies = client.getState().getCookies();
System.out.println("Cookies: ");
for (int i = 0; i < cookies.length; i++) {
System.out.println(" - " + cookies[i].toExternalForm());
}
System.out.println();
System.out.println("Indholdet af Siden: ");
System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
if (cookies.length>0) {
haveCookie = true;
} else {
System.out.println("Fik ingen Cookie");
haveCookie = false;
}
}
}
}
public static void main(String[] args) throws Exception {
GetPage get = new GetPage();
get.login("m00m000", "m0000000000000000");
get.get("
http://195.149.159.155/Common/menu.asp?timeOut=&loginError=&showMen
u=login");
}
}