Skip to content

Instantly share code, notes, and snippets.

@isopov
Created May 30, 2012 14:37
Show Gist options
  • Save isopov/2836705 to your computer and use it in GitHub Desktop.
Save isopov/2836705 to your computer and use it in GitHub Desktop.
LittleProxy testing
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sopovs.moradanen</groupId>
<artifactId>proxy-text</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- To create proxy -->
<dependency>
<groupId>org.littleshoot</groupId>
<artifactId>littleproxy</artifactId>
<version>0.4</version>
</dependency>
<!-- Too access web pages over proxy -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2</version>
</dependency>
</dependencies>
</project>
package com.sopovs.moradanen;
import java.io.IOException;
import java.util.Collections;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.littleshoot.proxy.DefaultHttpProxyServer;
import org.littleshoot.proxy.HttpFilter;
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.HttpRequestFilter;
public class Test1 {
private static final int PROXY_PORT = 10200;
public static void main(String[] args) throws HttpException, IOException {
//Whithout proxy
HttpClient client = new HttpClient();
client.getParams().setVersion(HttpVersion.HTTP_1_0);
GetMethod getMethod = new GetMethod("http://google.com");
//should print 200 - HTTP OK status
if (200 == client.executeMethod(getMethod)) {
System.out.println("OK without proxy");
}
//With proxy
HttpProxyServer proxy = new DefaultHttpProxyServer(PROXY_PORT, new HttpRequestFilter() {
public void filter(HttpRequest httpRequest) {
System.out.println("Request went through proxy");
}
}, Collections.<String, HttpFilter> emptyMap());
proxy.start();
System.out.println("proxy started");
HostConfiguration config = client.getHostConfiguration();
config.setProxy("localhost", PROXY_PORT);
if (200 == client.executeMethod(getMethod)) {
System.out.println("OK though proxy");
}
proxy.stop();
System.out.println("proxy stopped");
}
}
package com.sopovs.moradanen;
import java.util.Collections;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.littleshoot.proxy.DefaultHttpProxyServer;
import org.littleshoot.proxy.HttpFilter;
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.HttpRequestFilter;
public class Test2 {
private static final int PROXY_PORT = 10200;
public static void main(String args[]) throws Exception {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://www.google.com.ua");
HttpResponse responce = client.execute(get);
if (200 == responce.getStatusLine().getStatusCode()) {
System.out.println("OK without proxy");
}
EntityUtils.consume(responce.getEntity());
HttpProxyServer proxy = new DefaultHttpProxyServer(PROXY_PORT, new HttpRequestFilter() {
public void filter(HttpRequest httpRequest) {
System.out.println("Request went through proxy");
}
}, Collections.<String, HttpFilter> emptyMap());
proxy.start();
System.out.println("proxy started");
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost("localhost", PROXY_PORT));
responce = client.execute(get);
if (200 == responce.getStatusLine().getStatusCode()) {
System.out.println("OK with proxy");
}
EntityUtils.consume(responce.getEntity());
proxy.stop();
System.out.println("proxy stopped");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment