构造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")); }

java 发送带Basic Auth认证的http post请求的更多相关文章

  1. java 发送带Basic Auth认证的http post请求实例代码

    构造http header private static final String URL = "url"; private static final String APP_KEY ...

  2. 精讲RestTemplate第9篇-如何通过HTTP Basic Auth认证

    本文是精讲RestTemplate第9篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...

  3. java发送带附件的邮件

    /** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...

  4. iOS AFNetWorking下得Basic Auth认证请求方式

    我新入职了一家公司,做了一个项目,服务器的大哥说他采用的是Basic Auth认证请求方式,一般我们用的都是OAuth的认证方式,下面我们就对比一下这两种认证方式 百度百科得到如下 Basic Aut ...

  5. Http basic Auth 认证方式帮助类

    BasicAuthenticationUtil import java.io.IOException; import java.security.MessageDigest; import javax ...

  6. ios开发使用Basic Auth 认证方式

    http://blog.csdn.net/joonchen111/article/details/48447813 我们app的开发通常有2种认证方式   一种是Basic Auth,一种是OAuth ...

  7. Etcd安全配置之Basic Auth认证

    <中小团队落地配置中心详解>文章中我们介绍了如何基于Etcd+Confd构建配置中心,最后提到Etcd的安全问题时说了可以使用账号密码认证以达到安全访问的目的,究竟该如何开启认证以及怎么设 ...

  8. HTTP Basic auth认证

    Basic 概述 Basic 认证是HTTP 中非常简单的认证方式,因为简单,所以不是很安全,不过仍然非常常用. 当一个客户端向一个需要认证的HTTP服务器进行数据请求时,如果之前没有认证过,HTTP ...

  9. Java发送带附件的QQ邮箱

    由于腾讯公司给QQ邮箱增加了一个授权码的密码保护,导致之前网上很多代码都不能用,于是就自己敲了一份demo. 注意在密码那里可能需要授权码,具体设置:http://service.mail.qq.co ...

随机推荐

  1. 2016/9/21 leetcode 解题笔记 395.Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  2. [Shell]字符截取命令:cut, printf, awk, sed

    ------------------------------------------------------------------------------------------- [cut命令] ...

  3. 《HTML5秘籍》学习总结--2016年7月24日

    前段时间因为工作中看到同事使用了一个type为date的<input>元素,直接就形成了一个日期选择的表单控件,当时觉得很神奇,以为是什么插件,就问了同事是怎么做出来的,同事告诉我这是HT ...

  4. kenrnel 驱动中常用的宏

    http://blog.csdn.net/uruita/article/details/7263290 1. MODULE_DEVICE_TABLE (usb, skel_table);该宏生成一个名 ...

  5. CoreLocation框架的使用---定位,求两地距离

    前言: 在iOS开发中,有关导航,周边的开发,必须基于2个框架: Map Kit :用于地图展示 Core Location :用于地理定位   用户隐私的保护 从iOS 6开始,苹果在保护用户隐私方 ...

  6. 【温故Delphi】之编译与链接(转)

    本文基本转自“Delphi编译/链接过程” @瓢虫Monster,向瓢虫君学习,:) 下图展示了Delphi是怎样编译源文件,并把它们链接起来,最终形成可执行文件的. 当Delphi编译项目(Proj ...

  7. dos 加用户

    net user lipeng 1qaz3EDC /addnet user zhangnan 1qaz3EDC /addnet localgroup "Remote Desktop User ...

  8. net.sf.json.JSONObject 和org.json.JSONObject 的差别

    http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject  的差别. 一.创建jso ...

  9. ubuntu 14.04加入五笔输入法

    快捷键"ctrl+AIt+T",弹出终端,输入以下指令 //先卸载IBUS输入法 killall ibus-daemon sudo apt- get purge ibus ibus ...

  10. 重启EBS

    http://www.cnblogs.com/toowang/archive/2012/03/28/2421275.html 概述步骤(此操作没有停止数据库): 1.登陆FTP,终止应用:cd $CO ...