我是靠谱客的博主 勤奋烤鸡,这篇文章主要介绍java 发送带Basic Auth认证的http post请求,现在分享给大家,希望可以做个参考。

构造http header

复制代码
1
2
3
private static final String URL = "url"; private static final String APP_KEY = "key"; private static final String SECRET_KEY = "secret";

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
/** * 构造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; }

老方式:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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方式

添加认证

复制代码
1
HttpClientContext context = HttpClientContext.create();

 

复制代码
1
2
3
4
5
6
public void addUserOAuth(String username, String password) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); org.apache.http.auth.Credentials credentials = new org.apache.http.auth.UsernamePasswordCredentials(username, password); credsProvider.setCredentials(org.apache.http.auth.AuthScope.ANY, credentials); this.context.setCredentialsProvider(credsProvider); }

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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")); // 执行请求

  addUserOAuth("123", "456");

  

  // 执行请求

HttpResponse response = httpClient.execute(httpPost,context);

复制代码
1
// 打印执行结果 System.out.println(EntityUtils.toString(response.getEntity(), "utf-8")); }

 

最后

以上就是勤奋烤鸡最近收集整理的关于java 发送带Basic Auth认证的http post请求的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部