Search in sources :

Example 31 with FormBody

use of okhttp3.FormBody in project keleFanfou by kelefun.

the class OAuthUtil method extractHeader.

/**
 * 普通接口请求
 *
 * 如果请求存在Authorization的Header并且以OAuth开头,则其随后的参数, 所有的名字以oauth_ 和xauth_(用于XAuth)开头的参数都参与签名
 * 如果请求是GET, 则QueryString中所有的参数都参与签名
 * 如果请求是POST并且,Content-Type是application/x-www-form-urlencoded, 则所有的POST参数需要参加签名
 * 其他的POST请求,参数都不参加签名,包括multipart/form-data
 * @param request
 * @return
 */
public String extractHeader(Request request) {
    // 获取请求参数
    HttpUrl url = request.url();
    OAuthRequest oAuthRequest = new OAuthRequest();
    // post请求--FormUrlEncoded
    RequestBody rb = request.body();
    if (rb != null && rb instanceof FormBody) {
        FormBody fb = (FormBody) rb;
        for (int i = 0; i < fb.size(); i++) {
            // oAuthRequest.setOauthParameter(fb.encodedName(i),fb.encodedValue(i));//工具类OAuthUtil会统一编码
            oAuthRequest.setOauthParameter(fb.name(i), fb.value(i));
        }
    }
    // get请求
    for (int i = 0; i < url.querySize(); i++) {
        oAuthRequest.setOauthParameter(url.queryParameterName(i), url.queryParameterValue(i));
    }
    oAuthRequest.setVerb(request.method());
    oAuthRequest.setBaseUrl(extractBaseUrl(url));
    // oauth
    addOAuthParams(oAuthRequest);
    return extractHeader(oAuthRequest);
}
Also used : OAuthRequest(com.funstill.kelefun.http.OAuthRequest) FormBody(okhttp3.FormBody) HttpUrl(okhttp3.HttpUrl) RequestBody(okhttp3.RequestBody)

Example 32 with FormBody

use of okhttp3.FormBody in project YhLibraryForAndroid by android-coco.

the class PostFormRequest method buildRequestBody.

@Override
protected RequestBody buildRequestBody() {
    if (files == null || files.isEmpty()) {
        FormBody.Builder builder = new FormBody.Builder();
        addParams(builder);
        FormBody formBody = builder.build();
        return formBody;
    } else {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        addParams(builder);
        for (int i = 0; i < files.size(); i++) {
            PostFormBuilder.FileInput fileInput = files.get(i);
            RequestBody fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileInput.filename)), fileInput.file);
            builder.addFormDataPart(fileInput.key, fileInput.filename, fileBody);
        }
        return builder.build();
    }
}
Also used : MultipartBody(okhttp3.MultipartBody) PostFormBuilder(org.yh.library.okhttp.builder.PostFormBuilder) FormBody(okhttp3.FormBody) PostFormBuilder(org.yh.library.okhttp.builder.PostFormBuilder) RequestBody(okhttp3.RequestBody)

Example 33 with FormBody

use of okhttp3.FormBody in project buck by facebook.

the class DoctorReportHelper method uploadRequest.

public DoctorEndpointResponse uploadRequest(DoctorEndpointRequest request) {
    if (!doctorConfig.getEndpointUrl().isPresent()) {
        String errorMsg = String.format("Doctor endpoint URL is not set. Please set [%s] %s on your .buckconfig", DoctorConfig.DOCTOR_SECTION, DoctorConfig.URL_FIELD);
        return createErrorDoctorEndpointResponse(errorMsg);
    }
    byte[] requestJson;
    try {
        requestJson = objectMapper.writeValueAsBytes(request);
    } catch (JsonProcessingException e) {
        return createErrorDoctorEndpointResponse("Failed to encode request to JSON. " + "Reason: " + e.getMessage());
    }
    OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).readTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).writeTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).build();
    Response httpResponse;
    try {
        RequestBody requestBody;
        ImmutableMap<String, String> extraArgs = doctorConfig.getExtraRequestArgs();
        if (extraArgs.isEmpty()) {
            requestBody = RequestBody.create(JSON, requestJson);
        } else {
            FormBody.Builder formBody = new FormBody.Builder();
            formBody.add("data", new String(requestJson));
            for (Map.Entry<String, String> entry : extraArgs.entrySet()) {
                formBody.add(entry.getKey(), entry.getValue());
            }
            requestBody = formBody.build();
        }
        Request httpRequest = new Request.Builder().url(doctorConfig.getEndpointUrl().get()).post(requestBody).build();
        httpResponse = httpClient.newCall(httpRequest).execute();
    } catch (IOException e) {
        return createErrorDoctorEndpointResponse("Failed to perform the request to " + doctorConfig.getEndpointUrl().get() + ". Reason: " + e.getMessage());
    }
    try {
        if (httpResponse.isSuccessful()) {
            String body = new String(httpResponse.body().bytes(), Charsets.UTF_8);
            return objectMapper.readValue(body, DoctorEndpointResponse.class);
        }
        return createErrorDoctorEndpointResponse("Request was not successful.");
    } catch (JsonProcessingException e) {
        return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
    } catch (IOException e) {
        return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) FormBody(okhttp3.FormBody) DoctorEndpointRequest(com.facebook.buck.doctor.config.DoctorEndpointRequest) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) DoctorEndpointResponse(com.facebook.buck.doctor.config.DoctorEndpointResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) RequestBody(okhttp3.RequestBody)

Example 34 with FormBody

use of okhttp3.FormBody in project okhttputils by hongyangAndroid.

the class PostFormRequest method buildRequestBody.

@Override
protected RequestBody buildRequestBody() {
    if (files == null || files.isEmpty()) {
        FormBody.Builder builder = new FormBody.Builder();
        addParams(builder);
        FormBody formBody = builder.build();
        return formBody;
    } else {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        addParams(builder);
        for (int i = 0; i < files.size(); i++) {
            PostFormBuilder.FileInput fileInput = files.get(i);
            RequestBody fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileInput.filename)), fileInput.file);
            builder.addFormDataPart(fileInput.key, fileInput.filename, fileBody);
        }
        return builder.build();
    }
}
Also used : MultipartBody(okhttp3.MultipartBody) PostFormBuilder(com.zhy.http.okhttp.builder.PostFormBuilder) FormBody(okhttp3.FormBody) PostFormBuilder(com.zhy.http.okhttp.builder.PostFormBuilder) RequestBody(okhttp3.RequestBody)

Example 35 with FormBody

use of okhttp3.FormBody in project MVCHelper by LuckyJayce.

the class DeleteMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    FormBody.Builder builder = new FormBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.add(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    RequestBody formBody = builder.build();
    return new Request.Builder().url(url).delete(formBody);
}
Also used : FormBody(okhttp3.FormBody) RequestBody(okhttp3.RequestBody)

Aggregations

Request (okhttp3.Request)61 Response (okhttp3.Response)58 FormBody (okhttp3.FormBody)53 RequestBody (okhttp3.RequestBody)43 IOException (java.io.IOException)39 Call (okhttp3.Call)32 Callback (okhttp3.Callback)29 JSONObject (org.json.JSONObject)20 Map (java.util.Map)15 HttpUrl (okhttp3.HttpUrl)10 OkHttpClient (okhttp3.OkHttpClient)10 MultipartBody (okhttp3.MultipartBody)9 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)6 MediaType (okhttp3.MediaType)6 TypeToken (com.google.gson.reflect.TypeToken)4 File (java.io.File)3 DataInputStream (java.io.DataInputStream)2 List (java.util.List)2 ProgressRequestBody (me.ccrama.redditslide.util.ProgressRequestBody)2