java 發(fā)送帶Basic Auth認(rèn)證的http post請(qǐng)求實(shí)例代碼
構(gòu)造http header
private static final String URL = "url"; private static final String APP_KEY = "key"; private static final String SECRET_KEY = "secret";
/** * 構(gòu)造Basic Auth認(rèn)證頭信息 * * @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("要發(fā)送的數(shù)據(jù)" + JSON.toJSONString(pushObject)); StringEntity myEntity = new StringEntity(JSON.toJSONString(pushObject), ContentType.APPLICATION_JSON);// 構(gòu)造請(qǐng)求數(shù)據(jù) post.addHeader("Authorization", getHeader()); post.setEntity(myEntity);// 設(shè)置請(qǐng)求體 String responseContent = null; // 響應(yīng)內(nèi)容 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); // 設(shè)置請(qǐng)求的參數(shù) 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")); // 執(zhí)行請(qǐng)求 HttpResponse response = httpClient.execute(httpPost); // 打印執(zhí)行結(jié)果 System.out.println(EntityUtils.toString(response.getEntity(), "utf-8")); }
以上這篇java 發(fā)送帶Basic Auth認(rèn)證的http post請(qǐng)求實(shí)例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis-Plus查詢中如何排除標(biāo)識(shí)字段
這篇文章主要介紹了Mybatis-Plus查詢中排除標(biāo)識(shí)字段的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Java實(shí)戰(zhàn)之小蜜蜂擴(kuò)音器網(wǎng)上商城系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)簡單的小蜜蜂擴(kuò)音器網(wǎng)上商城系統(tǒng),文中采用到的技術(shù)有JSP、Servlet?、JDBC、Ajax等,感興趣的可以動(dòng)手試一試2022-03-03Javas使用Redlock實(shí)現(xiàn)分布式鎖過程解析
這篇文章主要介紹了Javas使用Redlock實(shí)現(xiàn)分布式鎖過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法
這篇文章主要介紹了Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08JavaWeb之Ajax的基本使用與實(shí)戰(zhàn)案例
ajax技術(shù)是使頁面能局部刷新的一種技術(shù),下面這篇文章主要給大家介紹了關(guān)于JavaWeb之Ajax的基本使用與實(shí)戰(zhàn)案例的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08java使用RestTemplate封裝post請(qǐng)求方式
這篇文章主要介紹了java使用RestTemplate封裝post請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10