[Java] HTTP Wrapper?
#1
Posted 31 May 2011 - 03:10 PM
#2
Posted 03 June 2011 - 03:12 PM
#3
Posted 03 June 2011 - 03:45 PM
#4
Posted 03 June 2011 - 05:09 PM
#6
Posted 03 June 2011 - 05:17 PM
I also need a wrapper for java
I get them for free. I'll make sure to save you one next time.
#7
Posted 03 June 2011 - 05:34 PM
#8
Posted 03 June 2011 - 05:39 PM
hhurrhurrhurI get them for free. I'll make sure to save you one next time.
Spoiler
PS: doesnt the java api take care of most of the work for an http client?
#9
Posted 03 June 2011 - 05:58 PM
http://hc.apache.org...nt-ga/tutorial/
#11
Posted 30 July 2011 - 01:30 PM
Add cookies to the http client:
public class NeopetsHttpClient extends HttpClient { public NeopetsHttpClient(NeopetAccount account) { getState().addCookies(account.getCookiesFromLogin()); }}
Sample post method:
public class NeopetsPostMethod extends PostMethod { public NeopetsPostMethod(String uri) { super(uri); setNecessaryParameters(); } public NeopetsPostMethod(String uri, String referrer) { super(uri); setNecessaryParameters(); addRequestHeader("Referer", referrer); } private void setNecessaryParameters() { addRequestHeader("Host", "www.neopets.com"); addRequestHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0"); addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); addRequestHeader("Accept-Encoding", "gzip, deflate"); addRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); addRequestHeader("Content-Type", "application/x-www-form-urlencoded"); }}Get method:
public class NeopetsGetMethod extends GetMethod { public NeopetsGetMethod(String uri) { super(uri); setNecessaryParameters(); } public NeopetsGetMethod(String uri, String referrer) { super(uri); setNecessaryParameters(); addRequestHeader("Referer", referrer); } private void setNecessaryParameters() { addRequestHeader("Host", "www.neopets.com"); addRequestHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0"); addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); addRequestHeader("Accept-Encoding", "gzip, deflate"); addRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); addRequestHeader("Content-Type", "application/x-www-form-urlencoded"); }}Simple login using the above:
public class NeopetsLogin { public NeopetAccount login(String userName, String password) throws LoginException { PostMethod postLoginMethod = new NeopetsPostMethod("http://www.neopets.com/login.phtml", "http://www.neopets.com/login/"); postLoginMethod.addParameter("destination", ""); postLoginMethod.addParameter("username", userName); postLoginMethod.addParameter("password", password); HttpClient postLoginClient = new HttpClient(); try { postLoginClient.executeMethod(postLoginMethod); } catch (IOException e) { throw new LoginException("Failed to login to neopets!"); } NeopetAccount account = new NeopetAccount(); account.setCookiesFromLogin(postLoginClient.getState().getCookies()); checkAccountContainsSSOToken(account); return account; } private void checkAccountContainsSSOToken(NeopetAccount account) throws LoginException { Cookie[] cookies = account.getCookiesFromLogin(); boolean containsSSOToken = false; for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase("ssotoken")) { containsSSOToken = true; } } if (!containsSSOToken) { throw new LoginException("Failed to login. Bad username/password combination."); } }}
#12
Posted 30 July 2011 - 01:56 PM
Heres some sample code using apaches http-commons (source here http://neopetsutils....c/neopetsutils/)
Add cookies to the http client:public class NeopetsHttpClient extends HttpClient { public NeopetsHttpClient(NeopetAccount account) { getState().addCookies(account.getCookiesFromLogin()); }}
Sample post method:public class NeopetsPostMethod extends PostMethod { public NeopetsPostMethod(String uri) { super(uri); setNecessaryParameters(); } public NeopetsPostMethod(String uri, String referrer) { super(uri); setNecessaryParameters(); addRequestHeader("Referer", referrer); } private void setNecessaryParameters() { addRequestHeader("Host", "www.neopets.com"); addRequestHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0"); addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); addRequestHeader("Accept-Encoding", "gzip, deflate"); addRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); addRequestHeader("Content-Type", "application/x-www-form-urlencoded"); }}Get method:public class NeopetsGetMethod extends GetMethod { public NeopetsGetMethod(String uri) { super(uri); setNecessaryParameters(); } public NeopetsGetMethod(String uri, String referrer) { super(uri); setNecessaryParameters(); addRequestHeader("Referer", referrer); } private void setNecessaryParameters() { addRequestHeader("Host", "www.neopets.com"); addRequestHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0"); addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); addRequestHeader("Accept-Encoding", "gzip, deflate"); addRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); addRequestHeader("Content-Type", "application/x-www-form-urlencoded"); }}Simple login using the above:public class NeopetsLogin { public NeopetAccount login(String userName, String password) throws LoginException { PostMethod postLoginMethod = new NeopetsPostMethod("http://www.neopets.com/login.phtml", "http://www.neopets.com/login/"); postLoginMethod.addParameter("destination", ""); postLoginMethod.addParameter("username", userName); postLoginMethod.addParameter("password", password); HttpClient postLoginClient = new HttpClient(); try { postLoginClient.executeMethod(postLoginMethod); } catch (IOException e) { throw new LoginException("Failed to login to neopets!"); } NeopetAccount account = new NeopetAccount(); account.setCookiesFromLogin(postLoginClient.getState().getCookies()); checkAccountContainsSSOToken(account); return account; } private void checkAccountContainsSSOToken(NeopetAccount account) throws LoginException { Cookie[] cookies = account.getCookiesFromLogin(); boolean containsSSOToken = false; for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase("ssotoken")) { containsSSOToken = true; } } if (!containsSSOToken) { throw new LoginException("Failed to login. Bad username/password combination."); } }}
Did you write those neopets utils?
#13
Posted 30 July 2011 - 04:18 PM
Did you write those neopets utils?
Yes, I wrote them. When I have more time I'll continue to better them and make a java suite.
Vince
#14
Posted 22 August 2011 - 09:23 PM
You can dig one out of there. As far as I know it's as fast as you'll ever need, as well as extremely flexible. Usage of the Page class isn't necessary, it just helps make far prettier code when you're building something as big as a framework
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users