国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java 發(fā)送帶Basic Auth認(rèn)證的http post請(qǐng)求實(shí)例代碼

2019-11-26 13:36:36
字體:
供稿:網(wǎng)友

構(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è)參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤庆县| 新安县| 芦溪县| 明星| 吐鲁番市| 禄劝| 都兰县| 凤翔县| 阳新县| 河北区| 蚌埠市| 额敏县| 赤城县| 安岳县| 阿克| 招远市| 修水县| 丽江市| 龙口市| 武冈市| 南和县| 中宁县| 大城县| 湘乡市| 辽宁省| 昭平县| 庆阳市| 铜梁县| 日土县| 云南省| 江口县| 汝州市| 阿荣旗| 临桂县| 宝清县| 江都市| 九龙坡区| 昌吉市| 彭阳县| 临高县| 册亨县|