有关Java调用第三方接口【Content-type为form-data】的示例代码

简介: 有关Java调用第三方接口【Content-type为form-data】的示例代码
// 创建HttpClient对象
HttpClient httpClient = HttpClientBuilder.create().build();
// 创建HttpPost对象,设置请求URL
HttpPost postRequest = new HttpPost("https://thirdparty.example/api"); 
// 创建MultipartEntityBuilder
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// 添加form-data参数
builder.addTextBody("username", "test"); 
builder.addTextBody("password", "123456");
// 构建HttpEntity对象
HttpEntity multipart = builder.build();
// 设置请求体
postRequest.setEntity(multipart);
// 设置Content-Type头
postRequest.setHeader("Content-Type", "multipart/form-data");
// 发送请求并获取响应
HttpResponse response = httpClient.execute(postRequest);
// 处理响应...

更优雅的方式

form-data的调用方式
HttpRequest.post(url)
       .form(")//表单内容 (可以使用map传参)
           .timeout(20000)//超时,毫秒
           .execute().body();
application/json的调用方式
HttpRequest.post(url)
            .header("Content-Type", "application/json")
            .header("token", "")
            .body(content)//使用JSON.toJSONString()序列化之后的数据
            .timeout(20000)
            .execute().body();
相关文章
|
5月前
|
JSON Java 数据格式
有关Java调用第三方接口【Content-type为form-data】的示例代码
有关Java调用第三方接口【Content-type为form-data】的示例代码
239 0
|
缓存 网络协议 前端开发
【Java】GET 和 POST 请求的区别
【Java】GET 和 POST 请求的区别
|
移动开发 Java
java发送post请求,使用multipart/form-data的方式传递参数
java发送post请求,使用multipart/form-data的方式传递参数
1362 0
|
1月前
|
JSON Java 数据格式
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
66 1
|
3月前
4. 解决uni-app开发过程中view、image等标签出现诸如“出现错误:类型“{ class: string; }”的参数不能赋给类型“.......”
4. 解决uni-app开发过程中view、image等标签出现诸如“出现错误:类型“{ class: string; }”的参数不能赋给类型“.......”
275 0
|
JSON 数据格式
解决POSTMAN传参报错,JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OB
解决POSTMAN传参报错,JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OB
解决POSTMAN传参报错,JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OB
|
Java 测试技术 API
1:Unit test and main function-Java API 实战
1:Unit test and main function-Java API 实战
100 0
1:Unit test and main function-Java API 实战
|
Java
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
400 0
|
JavaScript Java
Java Web——jQuery中的过滤器与第一组函数(val、text、attr)
Java Web——jQuery中的过滤器与第一组函数(val、text、attr)
Java Web——jQuery中的过滤器与第一组函数(val、text、attr)
|
JSON 前端开发 Java
【Json与Ajax交互报错解决】No converter found for return value of type: class com.github.pagehelper.PageInfo
【Json与Ajax交互报错解决】No converter found for return value of type: class com.github.pagehelper.PageInfo
321 0
【Json与Ajax交互报错解决】No converter found for return value of type: class com.github.pagehelper.PageInfo