org.zodiac.sdk.nio.channeling.http.HttpDemo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zodiac-sdk-nio Show documentation
Show all versions of zodiac-sdk-nio Show documentation
Zodiac SDK NIO2(New Non-Blocking IO)
package org.zodiac.sdk.nio.channeling.http;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import org.zodiac.sdk.nio.channeling.Channeling;
import org.zodiac.sdk.nio.channeling.ChannelingSocket;
import org.zodiac.sdk.nio.http.common.HttpRequestMethod;
public class HttpDemo {
public static void main(String[] args) throws IOException, InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
AtomicInteger totalDone = new AtomicInteger();
Channeling channeling = Channeling.startNewChanneling(1, 20000, 20000);
ChannelingSocket cs = channeling.wrap(null);
HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
requestBuilder.setMethod(HttpRequestMethod.GET);
requestBuilder.addHeader("Host", "tieba.baidu.com");
requestBuilder.setPath("/index.html");
HttpSingleRequest httpSingleRequest = new HttpSingleRequest(cs,
"tieba.baidu.com",
80,
requestBuilder.toString(),
1024);
httpSingleRequest.execute(new HttpSingleRequestCallback() {
@Override
public void error(Exception e, ChannelingSocket socket) {
e.printStackTrace();
}
@Override
public void accept(HttpResponse response, Object attachment) {
String result = response.getBodyContent();
if (!result.toLowerCase().contains("