Search in sources :

Example 31 with Builder

use of okhttp3.OkHttpClient.Builder in project FlareBot by FlareBot.

the class WebUtils method post.

public static Response post(Request.Builder builder) throws IOException {
    Response res = client.newCall(builder.build()).execute();
    ResponseBody body = res.body();
    if (res.code() >= 200 && res.code() < 300)
        return res;
    else
        throw new IllegalStateException("Failed to POST to '" + builder.build().url() + "'! Code: " + res.code() + ", Message: " + res.message() + ", Body: " + (body != null ? body.string().replace("\n", "").replace("\t", " ").replaceAll(" +", " ") : "null"));
}
Also used : Response(okhttp3.Response) ResponseBody(okhttp3.ResponseBody)

Example 32 with Builder

use of okhttp3.OkHttpClient.Builder in project EhViewer by seven332.

the class EhEngine method commentGallery.

public static GalleryComment[] commentGallery(@Nullable EhClient.Task task, OkHttpClient okHttpClient, String url, String comment) throws Exception {
    FormBody.Builder builder = new FormBody.Builder().add("commenttext_new", comment);
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).post(builder.build()).build();
    Call call = okHttpClient.newCall(request);
    // Put call
    if (null != task) {
        task.setCall(call);
    }
    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        Document document = Jsoup.parse(body);
        Elements elements = document.select("#chd + p");
        if (elements.size() > 0) {
            throw new EhException(elements.get(0).text());
        }
        return GalleryDetailParser.parseComments(document);
    } catch (Exception e) {
        throwException(call, code, headers, body, e);
        throw e;
    }
}
Also used : Call(okhttp3.Call) Headers(okhttp3.Headers) FormBody(okhttp3.FormBody) Request(okhttp3.Request) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) EhException(com.hippo.ehviewer.client.exception.EhException) ParseException(com.hippo.ehviewer.client.exception.ParseException) CancelledException(com.hippo.ehviewer.client.exception.CancelledException) NoHAtHClientException(com.hippo.ehviewer.client.exception.NoHAtHClientException) StatusCodeException(com.hippo.network.StatusCodeException) Response(okhttp3.Response) EhException(com.hippo.ehviewer.client.exception.EhException)

Example 33 with Builder

use of okhttp3.OkHttpClient.Builder in project EhViewer by seven332.

the class EhEngine method imageSearch.

/**
 * @param image Must be jpeg
 */
public static GalleryListParser.Result imageSearch(@Nullable EhClient.Task task, OkHttpClient okHttpClient, File image, boolean uss, boolean osc, boolean se) throws Exception {
    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);
    builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"sfile\"; filename=\"a.jpg\""), RequestBody.create(MEDIA_TYPE_JPEG, image));
    if (uss) {
        builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"fs_similar\""), RequestBody.create(null, "on"));
    }
    if (osc) {
        builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"fs_covers\""), RequestBody.create(null, "on"));
    }
    if (se) {
        builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"fs_exp\""), RequestBody.create(null, "on"));
    }
    builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"f_sfile\""), RequestBody.create(null, "File Search"));
    String url = EhUrl.getImageSearchUrl();
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).post(builder.build()).build();
    Call call = okHttpClient.newCall(request);
    // Put call
    if (null != task) {
        task.setCall(call);
    }
    String body = null;
    Headers headers = null;
    GalleryListParser.Result result;
    int code = -1;
    try {
        Response response = call.execute();
        Log.d(TAG, "" + response.request().url().toString());
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        result = GalleryListParser.parse(body);
    } catch (Exception e) {
        throwException(call, code, headers, body, e);
        throw e;
    }
    // Filter title and uploader
    List<GalleryInfo> list = result.galleryInfoList;
    for (int i = 0, n = list.size(); i < n; i++) {
        GalleryInfo info = list.get(i);
        if (!sEhFilter.filterTitle(info) || !sEhFilter.filterUploader(info)) {
            list.remove(i);
            i--;
            n--;
        }
    }
    if (list.size() > 0 && (Settings.getShowJpnTitle() || sEhFilter.needCallApi())) {
        // Fill by api
        fillGalleryListByApi(task, okHttpClient, list);
        // Filter tag
        for (int i = 0, n = list.size(); i < n; i++) {
            GalleryInfo info = list.get(i);
            if (!sEhFilter.filterTag(info) || !sEhFilter.filterTagNamespace(info)) {
                list.remove(i);
                i--;
                n--;
            }
        }
    }
    for (GalleryInfo info : list) {
        info.thumb = EhUrl.getFixedPreviewThumbUrl(info.thumb);
    }
    return result;
}
Also used : Call(okhttp3.Call) Headers(okhttp3.Headers) Request(okhttp3.Request) GalleryInfo(com.hippo.ehviewer.client.data.GalleryInfo) EhException(com.hippo.ehviewer.client.exception.EhException) ParseException(com.hippo.ehviewer.client.exception.ParseException) CancelledException(com.hippo.ehviewer.client.exception.CancelledException) NoHAtHClientException(com.hippo.ehviewer.client.exception.NoHAtHClientException) StatusCodeException(com.hippo.network.StatusCodeException) Response(okhttp3.Response) MultipartBody(okhttp3.MultipartBody) GalleryListParser(com.hippo.ehviewer.client.parser.GalleryListParser)

Example 34 with Builder

use of okhttp3.OkHttpClient.Builder in project EhViewer by seven332.

the class EhEngine method signIn.

public static String signIn(@Nullable EhClient.Task task, OkHttpClient okHttpClient, String username, String password, String recaptchaChallenge, String recaptchaResponse) throws Exception {
    FormBody.Builder builder = new FormBody.Builder().add("UserName", username).add("PassWord", password).add("submit", "Log me in").add("CookieDate", "1").add("temporary_https", "off");
    if (!TextUtils.isEmpty(recaptchaChallenge) && !TextUtils.isEmpty(recaptchaResponse)) {
        builder.add("recaptcha_challenge_field", recaptchaChallenge);
        builder.add("recaptcha_response_field", recaptchaResponse);
    }
    String url = EhUrl.API_SIGN_IN;
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).post(builder.build()).build();
    Call call = okHttpClient.newCall(request);
    // Put call
    if (null != task) {
        task.setCall(call);
    }
    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return SignInParser.parse(body);
    } catch (Exception e) {
        throwException(call, code, headers, body, e);
        throw e;
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Headers(okhttp3.Headers) FormBody(okhttp3.FormBody) Request(okhttp3.Request) EhException(com.hippo.ehviewer.client.exception.EhException) ParseException(com.hippo.ehviewer.client.exception.ParseException) CancelledException(com.hippo.ehviewer.client.exception.CancelledException) NoHAtHClientException(com.hippo.ehviewer.client.exception.NoHAtHClientException) StatusCodeException(com.hippo.network.StatusCodeException)

Example 35 with Builder

use of okhttp3.OkHttpClient.Builder in project EhViewer by seven332.

the class EhEngine method modifyFavorites.

public static FavoritesParser.Result modifyFavorites(@Nullable EhClient.Task task, OkHttpClient okHttpClient, String url, long[] gidArray, int dstCat, boolean callApi) throws Exception {
    String catStr;
    if (dstCat == -1) {
        catStr = "delete";
    } else if (dstCat >= 0 && dstCat <= 9) {
        catStr = "fav" + dstCat;
    } else {
        throw new EhException("Invalid dstCat: " + dstCat);
    }
    FormBody.Builder builder = new FormBody.Builder();
    builder.add("ddact", catStr);
    for (long gid : gidArray) {
        builder.add("modifygids[]", Long.toString(gid));
    }
    builder.add("apply", "Apply");
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).post(builder.build()).build();
    Call call = okHttpClient.newCall(request);
    // Put call
    if (null != task) {
        task.setCall(call);
    }
    String body = null;
    Headers headers = null;
    FavoritesParser.Result result;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        result = FavoritesParser.parse(body);
    } catch (Exception e) {
        throwException(call, code, headers, body, e);
        throw e;
    }
    if (callApi && result.galleryInfoList.size() > 0) {
        fillGalleryListByApi(task, okHttpClient, result.galleryInfoList);
    }
    for (GalleryInfo info : result.galleryInfoList) {
        info.thumb = EhUrl.getFixedPreviewThumbUrl(info.thumb);
    }
    return result;
}
Also used : Call(okhttp3.Call) Headers(okhttp3.Headers) FormBody(okhttp3.FormBody) Request(okhttp3.Request) GalleryInfo(com.hippo.ehviewer.client.data.GalleryInfo) EhException(com.hippo.ehviewer.client.exception.EhException) ParseException(com.hippo.ehviewer.client.exception.ParseException) CancelledException(com.hippo.ehviewer.client.exception.CancelledException) NoHAtHClientException(com.hippo.ehviewer.client.exception.NoHAtHClientException) StatusCodeException(com.hippo.network.StatusCodeException) EhException(com.hippo.ehviewer.client.exception.EhException) Response(okhttp3.Response) FavoritesParser(com.hippo.ehviewer.client.parser.FavoritesParser)

Aggregations

Request (okhttp3.Request)186 Response (okhttp3.Response)128 OkHttpClient (okhttp3.OkHttpClient)116 IOException (java.io.IOException)97 RequestBody (okhttp3.RequestBody)76 Test (org.junit.Test)68 File (java.io.File)41 MultipartBody (okhttp3.MultipartBody)40 HttpUrl (okhttp3.HttpUrl)39 Map (java.util.Map)34 MockResponse (okhttp3.mockwebserver.MockResponse)32 Call (okhttp3.Call)28 Interceptor (okhttp3.Interceptor)28 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)26 Retrofit (retrofit2.Retrofit)26 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)25 Builder (okhttp3.OkHttpClient.Builder)23 FormBody (okhttp3.FormBody)20 ResponseBody (okhttp3.ResponseBody)20 Builder (okhttp3.Request.Builder)19