public static String upload(String url, File file) throws IOException { OkHttpClient client = new OkHttpClient(); RequestBody formBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file)) .build(); Request request = new Request.Builder().url(url).post(formBody).build(); Response response = client.newCall(request).execute(); return response.body().string(); } //上传pdf文件 //System.out.println(upload("http://182.92.156.167:8081/jeeplus/api/customer/webOssuploads", new File("C:/test.txt")));
//get请求
OkHttpClient okHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder();
Request request = builder.get().url("http://a.laremehpe.xyz/php/dictionaryLookUp.php?showRecord=Throne&userName=test").build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
System.out.println(response.body().string());
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
}
});
标签:Request,OkHttpClient,call,file,NotNull,okhttp,new From: https://www.cnblogs.com/laremehpe/p/16776630.html