java 发送 Content-Type 为 multipartform-data 的post请求(RestTemplate, HttpClient, okhttp3)

tech2024-11-07  19

okhttp3 引入pom依赖 <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.0.0</version> </dependency>

代码示例

OkHttpClient client = new OkHttpClient().newBuilder() .build(); RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM) .addFormDataPart("from", "aaa@qq.com") .addFormDataPart("to", "bbb@qq.com") .addFormDataPart("subject", "test") .addFormDataPart("text", "aaaaa") .build(); Request request = new Request.Builder() .url("https://*****/*****/****/api/send ") .method("POST", body) .addHeader("Authorization", "Basic ********W5yeS1zZX******") .addHeader("Content-Type", "multipart/form-data") .build(); Response response = client.newCall(request).execute(); httpClient 引入pom依赖,按需取用 httpClient请求form-data形式, 切勿加 Content-type <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.2</version> </dependency>

代码示例

CloseableHttpClient client = HttpClients.createDefault(); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("from", "********", ContentType.create(ContentType.MULTIPART_FORM_DATA.getMimeType(), Charset.defaultCharset())); builder.addTextBody("to", "******", ContentType.create(ContentType.MULTIPART_FORM_DATA.getMimeType(), "utf-8")); builder.addTextBody("subject", "subject 测试标题", ContentType.create(ContentType.MULTIPART_FORM_DATA.getMimeType(), "utf-8")); builder.addTextBody("text", "text 测试邮件", ContentType.create(ContentType.MULTIPART_FORM_DATA.getMimeType(), "utf-8")); HttpEntity build = builder.build(); HttpPost httpPost = new HttpPost("https://******/******/send"); // httpPost.setHeader("Content-Type", "multipart/form-data"); String auth = Base64.getEncoder().encodeToString("aaaaaaaaaaaaaaa".getBytes()); httpPost.setHeader("Authorization", "Basic " + auth); httpPost.setEntity(build); HttpResponse httpResponse = null; try { httpResponse = client.execute(httpPost); } catch (IOException e) { e.printStackTrace(); } restTemplate 引入 spring-web 的依赖 代码示例 RestTemplate restTemplate = new RestTemplate(); String auth = Base64.getEncoder().encodeToString("************".getBytes()); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); httpHeaders.setBasicAuth(auth); MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>(); param.add("from", "*******"); param.add("to", "*********"); param.add("subject", "test"); param.add("text", "test"); HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(param, httpHeaders); // 执行HTTP请求 CloseableHttpResponse response = null; ResponseEntity<String> responseEntity = restTemplate.postForEntity("https://************/send", requestEntity, String.class); System.out.println(responseEntity);
最新回复(0)