| package de.aja.commons.dabbling; |
| |
| import java.io.BufferedInputStream; |
| import java.io.BufferedOutputStream; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.util.Properties; |
| |
| import org.apache.commons.httpclient.Cookie; |
| import org.apache.commons.httpclient.HttpClient; |
| import org.apache.commons.httpclient.HttpMethod; |
| import org.apache.commons.httpclient.HttpState; |
| import org.apache.commons.httpclient.HttpStatus; |
| import org.apache.commons.httpclient.NameValuePair; |
| import org.apache.commons.httpclient.cookie.CookiePolicy; |
| import org.apache.commons.httpclient.methods.GetMethod; |
| import org.apache.log4j.Logger; |
| |
| import de.aja.dabbling.commons.utils.DabblingHelpers; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class FirstTest { |
| |
| final static Logger logger = Logger |
| .getLogger(de.aja.commons.dabbling.FirstTest.class); |
| |
| |
| private String urlToRequest; |
| |
| |
| private HttpClient client; |
| |
| protected void setUrlToRequest(String urlToRequest) { |
| this.urlToRequest = urlToRequest; |
| } |
| |
| FirstTest() { |
| client = new HttpClient(); |
| |
| } |
| |
| |
| |
| |
| |
| void doGetRequest() { |
| logger.info("doGetRequest start."); |
| DabblingHelpers.setStartTime(); |
| HttpMethod method = new GetMethod(urlToRequest); |
| |
| |
| NameValuePair nvp1 = new NameValuePair("action", "recent"); |
| method.setQueryString(new NameValuePair[] { nvp1 }); |
| |
| |
| Properties props = new Properties(); |
| InputStream iStr = getClass().getClassLoader().getResourceAsStream( |
| "config.properties"); |
| try { |
| props.load(iStr); |
| } catch (IOException e1) { |
| logger |
| .error( |
| "Properties File \"config.properties\" konnte nicht geladen werden", |
| e1); |
| System.exit(0); |
| } catch (NullPointerException npe) { |
| logger |
| .error( |
| "Properties File \"config.properties\" konnte nicht gefunden werden.", |
| npe); |
| System.exit(0); |
| |
| } |
| |
| |
| Cookie cookie = new Cookie("217.160.137.156", "YaBBSE140usernamev14", |
| props.getProperty("user.cookie.YaBBSE140usernamev14"), "/", -1, |
| false); |
| HttpState initialState = new HttpState(); |
| initialState.addCookie(cookie); |
| initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY); |
| client.setState(initialState); |
| |
| try { |
| |
| int statusCode = client.executeMethod(method); |
| logger.info("Query String>>>=" + method.getQueryString()); |
| logger |
| .info("Status Text>>>" |
| + HttpStatus.getStatusText(statusCode)); |
| |
| |
| logger.info("Start Cookies------------------>"); |
| Cookie[] cookies = client.getState().getCookies(); |
| for (int i = 0; i < cookies.length; i++) { |
| logger.info("A Cookie"); |
| logger.info("Name=" + cookies[i].getName()); |
| logger.info("Value=" + cookies[i].getValue()); |
| logger.info("Domain=" + cookies[i].getDomain()); |
| } |
| logger.info("End Cookie<------------------"); |
| |
| InputStream is = new BufferedInputStream(method |
| .getResponseBodyAsStream()); |
| |
| |
| BufferedOutputStream bos = new BufferedOutputStream( |
| new FileOutputStream("genFiles\\atnotes.de")); |
| |
| byte[] buf = new byte[4096]; |
| int len; |
| while ((len = is.read(buf)) > 0) { |
| bos.write(buf, 0, len); |
| } |
| bos.close(); |
| is.close(); |
| method.releaseConnection(); |
| method = null; |
| |
| } catch (IOException e) { |
| logger.error(e); |
| |
| } catch (Throwable t) { |
| logger.error(t); |
| } |
| |
| finally { |
| if (method != null) { |
| logger |
| .debug("connection of object \"method\" closed in finally!"); |
| method.releaseConnection(); |
| |
| } |
| } |
| logger.info("doGetRequest end. This method took more or less:" |
| + DabblingHelpers.getTimeDifInSeconds() + " seconds to run."); |
| |
| } |
| |
| public static void main(String[] args) { |
| FirstTest fTest = new FirstTest(); |
| fTest.setUrlToRequest("http://www.atnotes.de/index.php"); |
| fTest.doGetRequest(); |
| } |
| } |