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