Java 发送HTTP-POST请求


`

      // 发送请求HTTP-POST请求 url:请求地址; entity:json格式请求参数
      public static String post(String url, String entity) {
	try {
              String username="user";// 账户
              String password="1234";// 密码		      
              CloseableHttpClient httpClient = HttpClients.createDefault();
              HttpPost httpPost = new HttpPost(url);
              httpPost.addHeader("Authorization", "Basic " + java.util.Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));
              StringEntity se = new StringEntity(entity, "UTF-8");
              se.setContentType("application/json");
              httpPost.setEntity(se);
              CloseableHttpResponse response = httpClient.execute(httpPost);
              HttpEntity entity1 = response.getEntity();
              String resStr = null;
              if (entity1 != null) {
                  resStr = EntityUtils.toString(entity1, "UTF-8");
              }
              httpClient.close();
              response.close();
              return resStr;
	  } catch (Exception e) {
              e.printStackTrace();
	  }
	  return "";
      }

`


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM