java 发送带Basic Auth认证的http post请求实例代码
作者:jingxian 时间:2021-11-03 06:21:20
构造http header
private static final String URL = "url";
private static final String APP_KEY = "key";
private static final String SECRET_KEY = "secret";
/**
* 构造Basic Auth认证头信息
*
* @return
*/
private String getHeader() {
String auth = APP_KEY + ":" + SECRET_KEY;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
return authHeader;
}
老方式:
private void send1(JPushObject pushObject) {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(URL);
System.out.println("要发送的数据" + JSON.toJSONString(pushObject));
StringEntity myEntity = new StringEntity(JSON.toJSONString(pushObject), ContentType.APPLICATION_JSON);// 构造请求数据
post.addHeader("Authorization", getHeader());
post.setEntity(myEntity);// 设置请求体
String responseContent = null; // 响应内容
CloseableHttpResponse response = null;
try {
response = client.execute(post);
System.out.println(JSON.toJSONString(response));
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
}
System.out.println("responseContent:" + responseContent);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (response != null)
response.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (client != null)
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
httpClient方式
public void send() throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = BaseHttpPost.buildHttpHeader(url);
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("fromAccid", fromAccid));
nvps.add(new BasicNameValuePair("toAccids", toAccids));
nvps.add(new BasicNameValuePair("type", msgType));
Map<String, Object> body = new HashMap<String, Object>();
body.put("msg", msg);
nvps.add(new BasicNameValuePair("body", JSON.toJSONString(body)));
nvps.add(new BasicNameValuePair("pushcontent", msg));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
// 打印执行结果
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
}
标签:java,发送,post,请求
0
投稿
猜你喜欢
Android 实现签到足迹功能
2023-06-21 15:16:24
C#实现中英文混合字符串截取的方法
2022-07-11 10:02:35
Spring Boot 集成PageHelper的使用方法
2021-10-04 19:16:30
Java集合的总体框架相关知识总结
2021-06-25 10:43:36
C#实现装箱与拆箱操作简单实例
2023-09-10 15:37:28
Spring @Retryable注解轻松搞定循环重试功能
2022-12-17 06:07:51
java8新特性-lambda表达式入门学习心得
2021-09-26 17:14:33
C#中split用法实例总结
2023-11-27 11:31:16
Json读写本地文件实现代码
2023-10-10 06:03:21
C#实现终止正在执行的线程
2022-05-06 22:34:22
Android中的常用尺寸单位(dp、sp)快速入门教程
2023-09-19 08:59:08
SpringCloud Feign参数问题及解决方法
2022-08-08 08:00:35
SVN出现提示org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir解决方案
2023-08-28 12:26:03
C#设置与获取环境变量的方法详解
2021-09-03 20:55:29
JAVA设计模式之责任链模式详解
2023-10-28 21:54:47
Springboot微服务打包Docker镜像流程解析
2022-02-23 19:39:00
c# Linq distinct不会调用Equals方法详解
2021-09-03 09:53:13
C#反射(Reflection)详解
2022-09-16 22:58:11
Java协议字节操作工具类详情
2023-11-02 06:25:02
详解SpringCloud微服务之Rest
2023-10-20 00:49:36