java 發送帶Basic Auth認證的http post請求執行個體代碼_java

來源:互聯網
上載者:User

構造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 發送帶Basic Auth認證的http post請求執行個體代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.