import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class DomeDome {
/*
* httpClient post请求
*/
// POST带参请求接口
public static void SendMailpost1() {
String strSend="";
CloseableHttpClient httpClient = HttpClients.createDefault();
// 普通请求 NameValuePair 类
List<NameValuePair> pairs=new ArrayList<NameValuePair>();
// Post请求方式
HttpPost httpPost = new HttpPost("接口地址");
// 添加普通参数
pairs.add(new BasicNameValuePair("key", "value"));
pairs.add(new BasicNameValuePair("key", "value"));
pairs.add(new BasicNameValuePair("key", "value"));
UrlEncodedFormEntity entity =new UrlEncodedFormEntity(pairs,Charset.forName("UTF-8"));
httpPost.setEntity(entity);
try {
// 请求接口获取服务器响应文件对象
HttpResponse response = httpClient.execute(httpPost);
// 获取响应报文
strSend = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (httpClient!=null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("post请求接口服务器响应的报文------>"+strSend);
}
}
//POST带参带附件请求接口
public static void SendMailpost(String url,String from, String to, String subject, String html, String appName, String secretKey,
String pdfFile,String BM_pdfname) {
File file = new File(pdfFile);
String strSend = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
// Post请求方式
HttpPost httpPost = new HttpPost(url);
// 涉及到附件上传, 需要使用 MultipartEntityBuilder类
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// 设置编码防止乱编
entity.setCharset(Charset.forName("UTF-8"));
// 定义设置参数编码
ContentType TEXT_PLAIN = ContentType.create("text/plain", Charset.forName("UTF-8"));
// 定义设置附件编码
ContentType OCTEC_STREAM = ContentType.create("application/octet-stream", Charset.forName("UTF-8"));
// 添加普通参数
entity.addTextBody("from", from, TEXT_PLAIN);
entity.addTextBody("to", to, TEXT_PLAIN);
entity.addTextBody("subject", subject, TEXT_PLAIN);
entity.addTextBody("html", html, TEXT_PLAIN);
entity.addTextBody("appName", appName, TEXT_PLAIN);
entity.addTextBody("secretKey", secretKey, TEXT_PLAIN);
// 添加附件参数
entity.addBinaryBody("attachments", file, OCTEC_STREAM, BM_pdfname);
// 把参数设置到请求数据中
httpPost.setEntity(entity.build());
try {
// 请求接口获取服务器响应文件对象
HttpResponse response = httpClient.execute(httpPost);
// 获取响应报文
strSend = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (httpClient!=null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("post请求接口服务器响应的报文------>"+strSend);
}
}
}
java httpClient post请求
最新推荐文章于 2024-07-14 21:53:24 发布