代码拉取完成,页面将自动刷新
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);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。