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;
}
});
}
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;
}
Aggregations