Skip to content

Instantly share code, notes, and snippets.

@codeachange
Created December 20, 2017 10:49
Show Gist options
  • Save codeachange/ad7ed91fb7d8432218fd4398ccf8a42b to your computer and use it in GitHub Desktop.
Save codeachange/ad7ed91fb7d8432218fd4398ccf8a42b to your computer and use it in GitHub Desktop.
async http client test
package com.example.test;
import org.asynchttpclient.*;
import java.net.ConnectException;
import java.util.concurrent.TimeoutException;
import static org.asynchttpclient.Dsl.asyncHttpClient;
import static org.asynchttpclient.Dsl.proxyServer;
public class AsyncHttpClientTest {
private static AsyncHttpClient asyncHttpClient;
private static int requestCount = 0;
public AsyncHttpClientTest() {
asyncHttpClient = asyncHttpClient();
}
public static void test() {
System.out.println(System.currentTimeMillis() + " start test");
int start = 27262976;
int end = 28311551;
final int max = 1000000;
while (start <= end && requestCount++ <= max) {
String ip = longToIp(start++);
doRequest(ip, 3128);
}
System.out.println(System.currentTimeMillis() + " end test");
}
private static void doRequest(String ip, int port) {
asyncHttpClient.prepareGet("http://myip.ipip.net/")
.setProxyServer(proxyServer(ip, port))
.setRequestTimeout(5000)
.execute(new MyAsyncHandler(ip));
}
private static String longToIp(long ipLong) {
return String.format("%d.%d.%d.%d",
(ipLong >> 24 & 0xff),
(ipLong >> 16 & 0xff),
(ipLong >> 8 & 0xff),
(ipLong & 0xff));
}
public static class MyAsyncHandler implements AsyncHandler<Response> {
private final Response.ResponseBuilder builder = new Response.ResponseBuilder();
private String proxyIp;
MyAsyncHandler(String proxyIp) {
this.proxyIp = proxyIp;
}
public void onThrowable(Throwable throwable) {
if (throwable instanceof TimeoutException) {
return;
}
if (throwable instanceof ConnectException && throwable.getMessage().contains("connection timed out")) {
return;
}
System.out.println(throwable);
throw new RuntimeException(throwable);
}
public AsyncHandler.State onBodyPartReceived(HttpResponseBodyPart httpResponseBodyPart) throws Exception {
builder.accumulate(httpResponseBodyPart);
return State.CONTINUE;
}
public AsyncHandler.State onStatusReceived(HttpResponseStatus httpResponseStatus) throws Exception {
builder.accumulate(httpResponseStatus);
return State.CONTINUE;
}
public AsyncHandler.State onHeadersReceived(HttpResponseHeaders httpResponseHeaders) throws Exception {
builder.accumulate(httpResponseHeaders);
return State.CONTINUE;
}
public Response onCompleted() throws Exception {
Response response = builder.build();
System.out.println("completed: " + proxyIp + " " + response.getStatusCode());
if (response.getResponseBody().contains(proxyIp)) {
System.out.println("good proxy:" + proxyIp);
}
return builder.build();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment