Search in sources :

Example 1 with CookieGroup

use of com.cv4j.netdiscovery.core.cookies.CookieGroup in project NetDiscovery by fengzhizi715.

the class UrlConnectionDownloader method download.

@Override
public Maybe<Response> download(Request request) {
    try {
        url = new URL(request.getUrl());
        // 设置Proxy
        if (request.getProxy() != null) {
            httpUrlConnection = (HttpURLConnection) url.openConnection(request.getProxy().toJavaNetProxy());
        } else {
            httpUrlConnection = (HttpURLConnection) url.openConnection();
        }
        // 使用Post请求时,设置Post body
        if (request.getHttpMethod() == HttpMethod.POST) {
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setDoInput(true);
            httpUrlConnection.setRequestMethod("POST");
            // post 请求不用缓存
            httpUrlConnection.setUseCaches(false);
            if (request.getHttpRequestBody() != null) {
                httpUrlConnection.setRequestProperty("Content-Type", request.getHttpRequestBody().getContentType());
                OutputStream os = httpUrlConnection.getOutputStream();
                os.write(request.getHttpRequestBody().getBody());
                os.flush();
                os.close();
            }
        }
        // 设置请求头header
        if (Preconditions.isNotBlank(request.getHeader())) {
            for (Map.Entry<String, String> entry : request.getHeader().entrySet()) {
                httpUrlConnection.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
        // 设置字符集
        if (Preconditions.isNotBlank(request.getCharset())) {
            httpUrlConnection.setRequestProperty("Accept-Charset", request.getCharset());
        }
        httpUrlConnection.connect();
        return Maybe.create(new MaybeOnSubscribe<InputStream>() {

            @Override
            public void subscribe(MaybeEmitter<InputStream> emitter) throws Exception {
                emitter.onSuccess(httpUrlConnection.getInputStream());
            }
        }).map(new Function<InputStream, Response>() {

            @Override
            public Response apply(InputStream inputStream) throws Exception {
                Response response = new Response();
                response.setContent(IOUtils.readInputStream(inputStream));
                response.setStatusCode(httpUrlConnection.getResponseCode());
                response.setContentType(httpUrlConnection.getContentType());
                if (request.isSaveCookie()) {
                    // save cookies
                    if (Preconditions.isNotBlank(httpUrlConnection.getHeaderField(Constant.SET_COOKIES_HEADER))) {
                        CookieGroup cookieGroup = CookieManager.getInsatance().getCookieGroup(request.getUrlParser().getHost());
                        if (cookieGroup == null) {
                            cookieGroup = new CookieGroup(request.getUrlParser().getHost());
                            String cookieStr = httpUrlConnection.getHeaderField(Constant.SET_COOKIES_HEADER);
                            String[] segs = cookieStr.split(";");
                            if (Preconditions.isNotBlank(segs)) {
                                for (String seg : segs) {
                                    String[] pairs = seg.trim().split("\\=");
                                    if (pairs.length == 2) {
                                        cookieSet.add(new Cookie(pairs[0], pairs[1]));
                                    }
                                }
                            }
                            cookieGroup.putAllCookies(cookieSet);
                            CookieManager.getInsatance().addCookieGroup(cookieGroup);
                        } else {
                            String cookieStr = httpUrlConnection.getHeaderField(Constant.SET_COOKIES_HEADER);
                            String[] segs = cookieStr.split(";");
                            if (Preconditions.isNotBlank(segs)) {
                                for (String seg : segs) {
                                    String[] pairs = seg.trim().split("\\=");
                                    if (pairs.length == 2) {
                                        cookieSet.add(new Cookie(pairs[0], pairs[1]));
                                    }
                                }
                            }
                            cookieGroup.putAllCookies(cookieSet);
                        }
                    }
                }
                return response;
            }
        });
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Cookie(com.cv4j.netdiscovery.core.cookies.Cookie) MalformedURLException(java.net.MalformedURLException) MaybeEmitter(io.reactivex.MaybeEmitter) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Response(com.cv4j.netdiscovery.core.domain.Response) CookieGroup(com.cv4j.netdiscovery.core.cookies.CookieGroup) MaybeOnSubscribe(io.reactivex.MaybeOnSubscribe) Map(java.util.Map)

Example 2 with CookieGroup

use of com.cv4j.netdiscovery.core.cookies.CookieGroup in project NetDiscovery by fengzhizi715.

the class VertxDownloader method download.

public Maybe<Response> download(Request request) {
    WebClientOptions options = initWebClientOptions(request);
    webClient = WebClient.create(vertx, options);
    HttpRequest<Buffer> httpRequest = null;
    if (request.getHttpMethod() == HttpMethod.GET) {
        if ("http".equals(request.getUrlParser().getProtocol())) {
            httpRequest = webClient.getAbs(request.getUrl());
        } else if ("https".equals(request.getUrlParser().getProtocol())) {
            httpRequest = webClient.get(443, request.getUrlParser().getHost(), Preconditions.isNotBlank(request.getUrlParser().getQuery()) ? request.getUrlParser().getPath() + "?" + request.getUrlParser().getQuery() : request.getUrlParser().getPath()).ssl(true);
        }
    } else if (request.getHttpMethod() == HttpMethod.POST) {
        if ("http".equals(request.getUrlParser().getProtocol())) {
            httpRequest = webClient.postAbs(request.getUrl());
        } else if ("https".equals(request.getUrlParser().getProtocol())) {
            httpRequest = webClient.post(443, request.getUrlParser().getHost(), Preconditions.isNotBlank(request.getUrlParser().getQuery()) ? request.getUrlParser().getPath() + "?" + request.getUrlParser().getQuery() : request.getUrlParser().getPath()).ssl(true);
        }
    }
    // 设置请求头header
    if (Preconditions.isNotBlank(header)) {
        for (Map.Entry<String, String> entry : header.entrySet()) {
            httpRequest.putHeader(entry.getKey(), entry.getValue());
        }
    }
    // 针对post请求,需要对header添加一些信息
    if (request.getHttpMethod() == HttpMethod.POST) {
        if (Preconditions.isNotBlank(request.getHttpRequestBody()) && Preconditions.isNotBlank(request.getHttpRequestBody().getContentType())) {
            httpRequest.putHeader("Content-type", request.getHttpRequestBody().getContentType());
        }
    }
    String charset = null;
    if (Preconditions.isNotBlank(request.getCharset())) {
        charset = request.getCharset();
    } else {
        charset = Constant.UTF_8;
    }
    HttpRequest<String> stringHttpRequest = httpRequest.as(BodyCodec.string(charset));
    Single<HttpResponse<String>> httpResponseSingle = null;
    if (request.getHttpMethod() == HttpMethod.GET) {
        httpResponseSingle = stringHttpRequest.rxSend();
    } else if (request.getHttpMethod() == HttpMethod.POST) {
        if (Preconditions.isNotBlank(request.getHttpRequestBody())) {
            Buffer buffer = Buffer.buffer();
            buffer.getDelegate().appendBytes(request.getHttpRequestBody().getBody());
            httpResponseSingle = stringHttpRequest.rxSendBuffer(buffer);
        } else {
            httpResponseSingle = stringHttpRequest.rxSend();
        }
    }
    return httpResponseSingle.toMaybe().map(new Function<HttpResponse<String>, Response>() {

        @Override
        public Response apply(HttpResponse<String> stringHttpResponse) throws Exception {
            String html = stringHttpResponse.body();
            Response response = new Response();
            response.setContent(html.getBytes());
            response.setStatusCode(stringHttpResponse.statusCode());
            response.setContentType(stringHttpResponse.getHeader("Content-Type"));
            if (request.isSaveCookie()) {
                // save cookies
                if (Preconditions.isNotBlank(stringHttpResponse.cookies())) {
                    CookieGroup cookieGroup = CookieManager.getInsatance().getCookieGroup(request.getUrlParser().getHost());
                    if (cookieGroup == null) {
                        cookieGroup = new CookieGroup(request.getUrlParser().getHost());
                        for (String cookieStr : stringHttpResponse.cookies()) {
                            String[] segs = cookieStr.split(";");
                            if (Preconditions.isNotBlank(segs)) {
                                for (String seg : segs) {
                                    String[] pairs = seg.trim().split("\\=");
                                    if (pairs.length == 2) {
                                        cookieSet.add(new Cookie(pairs[0], pairs[1]));
                                    }
                                }
                            }
                        }
                        cookieGroup.putAllCookies(cookieSet);
                        CookieManager.getInsatance().addCookieGroup(cookieGroup);
                    } else {
                        for (String cookieStr : stringHttpResponse.cookies()) {
                            String[] segs = cookieStr.split(";");
                            if (Preconditions.isNotBlank(segs)) {
                                for (String seg : segs) {
                                    String[] pairs = seg.trim().split("\\=");
                                    if (pairs.length == 2) {
                                        cookieSet.add(new Cookie(pairs[0], pairs[1]));
                                    }
                                }
                            }
                        }
                        cookieGroup.putAllCookies(cookieSet);
                    }
                }
            }
            return response;
        }
    });
}
Also used : Buffer(io.vertx.reactivex.core.buffer.Buffer) Cookie(com.cv4j.netdiscovery.core.cookies.Cookie) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) Response(com.cv4j.netdiscovery.core.domain.Response) CookieGroup(com.cv4j.netdiscovery.core.cookies.CookieGroup) Map(java.util.Map)

Aggregations

Cookie (com.cv4j.netdiscovery.core.cookies.Cookie)2 CookieGroup (com.cv4j.netdiscovery.core.cookies.CookieGroup)2 Response (com.cv4j.netdiscovery.core.domain.Response)2 Map (java.util.Map)2 MaybeEmitter (io.reactivex.MaybeEmitter)1 MaybeOnSubscribe (io.reactivex.MaybeOnSubscribe)1 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)1 Buffer (io.vertx.reactivex.core.buffer.Buffer)1 HttpResponse (io.vertx.reactivex.ext.web.client.HttpResponse)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1