All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sdk.nio.channeling.http.HttpDemo Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
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("")) {
                    throw new IllegalArgumentException(result.substring(result.length() - 15));
                }
                totalDone.incrementAndGet();
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        if (totalDone.get() % 100 == 0)
            System.out.println("Done " + totalDone.get());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy