Search in sources :

Example 1 with HttpRequestBody

use of com.cv4j.netdiscovery.core.domain.HttpRequestBody in project NetDiscovery by fengzhizi715.

the class OkHttpDownloader method download.

@Override
public Maybe<Response> download(Request request) {
    okhttp3.Request.Builder requestBuilder = null;
    if (request.getHttpMethod() == HttpMethod.GET) {
        requestBuilder = new okhttp3.Request.Builder().url(request.getUrl());
    } else if (request.getHttpMethod() == HttpMethod.POST) {
        HttpRequestBody httpRequestBody = request.getHttpRequestBody();
        if (httpRequestBody != null) {
            MediaType mediaType = MediaType.parse(httpRequestBody.getContentType());
            // 创建RequestBody对象,将参数按照指定的MediaType封装
            RequestBody requestBody = RequestBody.create(mediaType, httpRequestBody.getBody());
            requestBuilder = new okhttp3.Request.Builder().url(request.getUrl()).post(requestBody);
        }
    }
    if (request.getHeader() != null) {
        for (Map.Entry<String, String> entry : request.getHeader().entrySet()) {
            requestBuilder.addHeader(entry.getKey(), entry.getValue());
        }
    }
    // 针对post请求,需要对header添加一些信息
    if (request.getHttpMethod() == HttpMethod.POST) {
        if (Preconditions.isNotBlank(request.getHttpRequestBody()) && Preconditions.isNotBlank(request.getHttpRequestBody().getContentType())) {
            requestBuilder.addHeader("Content-type", request.getHttpRequestBody().getContentType());
        }
    }
    okhttp3.Request okrequest = requestBuilder.build();
    return Maybe.create(new MaybeOnSubscribe<okhttp3.Response>() {

        @Override
        public void subscribe(MaybeEmitter emitter) throws Exception {
            emitter.onSuccess(client.newCall(okrequest).execute());
        }
    }).map(new Function<okhttp3.Response, Response>() {

        @Override
        public Response apply(okhttp3.Response resp) throws Exception {
            Response response = new Response();
            response.setContent(resp.body().bytes());
            response.setStatusCode(resp.code());
            response.setContentType(resp.header("Content-Type"));
            return response;
        }
    });
}
Also used : MaybeEmitter(io.reactivex.MaybeEmitter) Request(com.cv4j.netdiscovery.core.domain.Request) IOException(java.io.IOException) Response(com.cv4j.netdiscovery.core.domain.Response) HttpRequestBody(com.cv4j.netdiscovery.core.domain.HttpRequestBody) MediaType(okhttp3.MediaType) MaybeOnSubscribe(io.reactivex.MaybeOnSubscribe) Map(java.util.Map) RequestBody(okhttp3.RequestBody) HttpRequestBody(com.cv4j.netdiscovery.core.domain.HttpRequestBody)

Example 2 with HttpRequestBody

use of com.cv4j.netdiscovery.core.domain.HttpRequestBody in project NetDiscovery by fengzhizi715.

the class Utils method getCaptcha.

/**
 * 返回验证码的内容
 * @param imageUrl 验证码的url
 * @param proxy
 * @return
 */
public static String getCaptcha(String imageUrl, Proxy proxy) {
    try {
        URL url = new URL("http://47.97.7.119:8018/captcha");
        HttpURLConnection httpUrlConnection = null;
        // 设置Proxy
        if (proxy != null) {
            httpUrlConnection = (HttpURLConnection) url.openConnection(proxy.toJavaNetProxy());
        } else {
            httpUrlConnection = (HttpURLConnection) url.openConnection();
        }
        httpUrlConnection.setDoOutput(true);
        httpUrlConnection.setDoInput(true);
        httpUrlConnection.setRequestMethod("POST");
        // post 请求不用缓存
        httpUrlConnection.setUseCaches(false);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("imageSource", imageUrl);
        HttpRequestBody body = HttpRequestBody.json(jsonObject);
        httpUrlConnection.setRequestProperty("Content-Type", HttpRequestBody.ContentType.JSON);
        OutputStream os = httpUrlConnection.getOutputStream();
        os.write(body.getBody());
        os.flush();
        os.close();
        httpUrlConnection.connect();
        String response = IOUtils.inputStream2String(httpUrlConnection.getInputStream());
        if (Preconditions.isNotBlank(response)) {
            JSONObject json = JSON.parseObject(response);
            return (String) json.get("text");
        }
        return null;
    } catch (IOException e) {
    }
    return null;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpRequestBody(com.cv4j.netdiscovery.core.domain.HttpRequestBody) JSONObject(com.alibaba.fastjson.JSONObject) URL(java.net.URL)

Aggregations

HttpRequestBody (com.cv4j.netdiscovery.core.domain.HttpRequestBody)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Request (com.cv4j.netdiscovery.core.domain.Request)1 Response (com.cv4j.netdiscovery.core.domain.Response)1 MaybeEmitter (io.reactivex.MaybeEmitter)1 MaybeOnSubscribe (io.reactivex.MaybeOnSubscribe)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Map (java.util.Map)1 MediaType (okhttp3.MediaType)1 RequestBody (okhttp3.RequestBody)1