use of forpdateam.ru.forpda.api.NetworkRequest in project ForPDA by RadiationX.
the class Client method prepareRequest.
private Request.Builder prepareRequest(NetworkRequest request, ProgressListener uploadProgressListener) {
String url = request.getUrl();
if (request.getUrl().substring(0, 2).equals("//")) {
url = "https:".concat(request.getUrl());
}
Log.d(LOG_TAG, "Request url " + request.getUrl());
Request.Builder requestBuilder = new Request.Builder().url(url).header("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4").header("User-Agent", USER_AGENT);
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
Log.d(LOG_TAG, "Header " + entry.getKey() + " : " + (privateHeaders.contains(entry.getKey()) ? "private" : entry.getValue()));
requestBuilder.header(entry.getKey(), entry.getValue());
}
}
if (request.getFormHeaders() != null || request.getFile() != null) {
Log.d(LOG_TAG, "Multipart " + request.isMultipartForm());
if (request.getFormHeaders() != null) {
for (Map.Entry<String, String> entry : request.getFormHeaders().entrySet()) {
Log.d(LOG_TAG, "Form header " + entry.getKey() + " : " + (privateHeaders.contains(entry.getKey()) ? "private" : entry.getValue()));
}
}
if (request.getFile() != null) {
Log.d(LOG_TAG, "Form file " + request.getFile().toString());
}
if (!request.isMultipartForm()) {
if (request.getFormHeaders() != null) {
FormBody.Builder formBuilder = new FormBody.Builder();
for (Map.Entry<String, String> entry : request.getFormHeaders().entrySet()) {
formBuilder.add(entry.getKey(), entry.getValue());
if (request.getEncodedFormHeaders() != null && request.getEncodedFormHeaders().contains(entry.getKey())) {
formBuilder.addEncoded(entry.getKey(), entry.getValue());
} else {
formBuilder.add(entry.getKey(), entry.getValue());
}
}
FormBody formBody = formBuilder.build();
requestBuilder.post(formBody);
}
} else {
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
if (request.getFormHeaders() != null) {
for (Map.Entry<String, String> entry : request.getFormHeaders().entrySet()) {
multipartBuilder.addFormDataPart(entry.getKey(), entry.getValue());
}
}
if (request.getFile() != null) {
MediaType type = MediaType.parse(request.getFile().getMimeType());
RequestBody requestBody = RequestBodyUtil.create(type, request.getFile().getFileStream());
multipartBuilder.addFormDataPart(request.getFile().getRequestName(), request.getFile().getFileName(), requestBody);
}
MultipartBody multipartBody = multipartBuilder.build();
if (uploadProgressListener == null) {
requestBuilder.post(multipartBody);
} else {
requestBuilder.post(new ProgressRequestBody(multipartBody, uploadProgressListener));
}
}
}
return requestBuilder;
}
use of forpdateam.ru.forpda.api.NetworkRequest in project ForPDA by RadiationX.
the class Client method request.
public NetworkResponse request(NetworkRequest request, OkHttpClient client, ProgressListener uploadProgressListener) throws Exception {
Request.Builder requestBuilder = prepareRequest(request, uploadProgressListener);
NetworkResponse response = new NetworkResponse(request.getUrl());
Response okHttpResponse = null;
try {
okHttpResponse = client.newCall(requestBuilder.build()).execute();
if (!okHttpResponse.isSuccessful()) {
if (okHttpResponse.code() == 403) {
String content = okHttpResponse.body().string();
// forpdateam.ru.forpda.utils.ApiUtils.longLog(content);
new Handler(Looper.getMainLooper()).post(() -> {
try {
if (TabManager.get().getTagContainClass(GoogleCaptchaFragment.class) == null) {
Bundle args = new Bundle();
args.putString("content", content);
TabManager.get().add(GoogleCaptchaFragment.class, args);
}
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
throw new OkHttpResponseException(okHttpResponse.code(), okHttpResponse.message(), request.getUrl());
}
response.setCode(okHttpResponse.code());
response.setMessage(okHttpResponse.message());
response.setRedirect(okHttpResponse.request().url().toString());
if (!request.isWithoutBody()) {
response.setBody(okHttpResponse.body().string());
getCounts(response.getBody());
checkForumErrors(response.getBody());
}
Log.d(LOG_TAG, "Response: " + response.toString());
} finally {
if (okHttpResponse != null)
okHttpResponse.close();
}
return response;
}
use of forpdateam.ru.forpda.api.NetworkRequest in project ForPDA by RadiationX.
the class Theme method reportPost.
public String reportPost(int topicId, int postId, String message) throws Exception {
NetworkRequest request = new NetworkRequest.Builder().url("https://4pda.ru/forum/index.php?act=report&send=1&t=" + Integer.toString(topicId) + "&p=" + Integer.toString(postId)).formHeader("message", URLEncoder.encode(message, "windows-1251"), true).build();
NetworkResponse response = Api.getWebClient().request(request);
Pattern p = Pattern.compile("<div class=\"errorwrap\">\n" + "\\s*<h4>Причина:</h4>\n" + "\\s*\n" + "\\s*<p>(.*)</p>", Pattern.MULTILINE);
Matcher m = p.matcher(response.getBody());
return m.find() ? "Ошибка отправки жалобы: ".concat(m.group(1)) : "Жалоба отправлена";
}
Aggregations