Ai
1 Star 0 Fork 0

NUT/Study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
HttpClientUtil 3.58 KB
一键复制 编辑 原始数据 按行查看 历史
NUT 提交于 2024-07-23 15:56 +08:00 . HttpClient multipart/form-data与 json格式参数
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.Charset;
public class HttpClientUtil {
private static final CloseableHttpClient httpclient = HttpClients.createDefault();
/**
* 发送 multipart/form-data 格式参数
* @param url
* @param issueDate
* @return
*/
public static String httpClientPost(String url,String issueDate) {
String res = null;
CloseableHttpResponse response = null;
// HttpPost httpPost = new HttpPost("http://11.65.178.35:12346/homeScreen/processingTime/2024/getProcessingTimeData");
HttpPost httpPost = new HttpPost(url);
String boundary ="--------------4585696313564699";
httpPost.setHeader("Content-Type","multipart/form-data; boundary="+boundary);
//创建 MultipartEntityBuilder,以此来构建我们的参数
MultipartEntityBuilder EntityBuilder = MultipartEntityBuilder.create();
//设置字符编码,防止乱码
ContentType contentType=ContentType.create("text/plain", Charset.forName("UTF-8"));
//遍历json参数
// for(String str:json.keySet()){
// EntityBuilder.addPart(str, new StringBody(json.get(str).toString(),contentType));
// }
// EntityBuilder.addPart("file",new FileBody(file));
if(NonUtil.isNotNon(issueDate)){
EntityBuilder.addPart("issueDate",new StringBody(issueDate,contentType));
}
//模拟浏览器
EntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
//boundary
EntityBuilder.setBoundary(boundary);
httpPost.setEntity(EntityBuilder.build());
try {
response = httpclient.execute(httpPost);
HttpEntity entity2 = response.getEntity();
res = EntityUtils.toString(entity2, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
return "错误error";
}
return res;
}
/**
* 发送 JSON 格式参数
* @param url
* @param issueDate
* @return
*/
public static String httpClientPostJson(String url, JSONObject json) {
HttpPost post = new HttpPost(url);
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = httpclient.execute(post);
String ret = "";
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity entity = res.getEntity();
ret = EntityUtils.toString(res.getEntity());// 返回json格式:
}
return ret;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tjcumath/study.git
git@gitee.com:tjcumath/study.git
tjcumath
study
Study
master

搜索帮助