构造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方式

添加认证

HttpClientContext context = HttpClientContext.create();
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);
}
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);

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

最新文章

  1. PID控制
  2. CozyRSS开发记录14-RSS源管理初步完工
  3. 我的第一个hadoop程序
  4. saltstack配置安装的一些关键步骤及安装时各种报错的分析
  5. DIV内英文或者数字不换行的问题 解决办法
  6. Code First 更新数据库结构(简单实现方法:会删除原来的数据)
  7. NameValueCollection类
  8. Android 三大图片缓存原理、特性对比
  9. HDU 4868 Information Extraction(2014 多校联合第一场 H)
  10. CSS 简介、语法、派生选择器、id 选择器、类选择器、属性选择器
  11. FastJson将json解析成含有泛型对象,内部泛型对象再次解析出错的解决办法(Android)
  12. 配置Meld为git的默认比较工具
  13. 初读&quot;Thinking in Java&quot;读书笔记之第九章 --- 接口
  14. 怎么重置mysql的自增列AUTO_INCREMENT初时值
  15. laravel 5.5 跨域问题解决方案
  16. Teamwork(The fourth day of the team)
  17. Java WebService Axis 初探
  18. NPOI 设置excel 边框
  19. addClass+siblings+removeClass用意:
  20. android开发问题 Failed to pull selection 菜鸟记录

热门文章

  1. jQueryEasyUI
  2. 【Visual Lisp】表处理专题
  3. iOS开发 multipart 上传多张图片
  4. hdu 5102 树上前k短路径长度和
  5. Heartbeat的两个小BUG
  6. struts 标签&lt;s:ierator&gt;的简单使用说明
  7. JS DOM操作
  8. 跟我一起学WCF(2)——利用.NET Remoting技术开发分布式应用
  9. 设计模式之美:Adapter(适配器)
  10. EntityFramework中使用Repository装饰器