https authorization basic

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ndkey.auditproxy.cityon;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author zxf
 */
public class TestMain {

    private static CloseableHttpClient httpClient = null;
    private final static ObjectMapper _objectMapper = new ObjectMapper();

    public static void main(String[] args) throws IOException, Exception {
        System.setProperty("jsse.enableSNIExtension", "false");
        SSLContext sslContext = SSLContext.getInstance("TLSv1.2");//TLS
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("wifi", "12345678"));
        httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(credsProvider).build();
        //      httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        CloseableHttpResponse response = null;
        try {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("name", "123");
            map.put("mobile", "18600000006");
            HttpPost httpPost = new HttpPost("https://uat15.acxiom.com.cn/cityon/resources/customer");
            String info = _objectMapper.writeValueAsString(map);
            //  httpPost.addHeader("Content-Type", "application/json");
            //  httpPost.addHeader("Authorization", "Basic " + Base64.encode("wifi:12345678".getBytes()));
        //    httpPost.setConfig(RequestConfig.custom().setConnectTimeout(5 * 1000).setConnectionRequestTimeout(5 * 10000).build());
            httpPost.setEntity(new StringEntity(info, ContentType.APPLICATION_JSON));
            response = httpClient.execute(httpPost);
            String res = EntityUtils.toString(response.getEntity());

            System.out.println("回复消息:");
            System.out.println(res);
        } catch (Exception ex) {
            throw new Exception(ex);
        } finally {
            if (response != null) {
                response.close();
            }
        }

        try {
            httpClient.close();
        } catch (IOException ex) {

        }
    }
    private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[]{};
        }

        public void checkClientTrusted(X509Certificate[] chain,
                String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain,
                String authType) throws CertificateException {
        }
    }};
}
时间: 2024-12-31 03:33:50

https authorization basic的相关文章

http authorization basic请求代码示例

/** * */ package testJava.java; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; /** * @author xxx * @function * @date 2016年4

The OAuth 2.0 Authorization Framework-摘自https://tools.ietf.org/html/rfc6749

Internet Engineering Task Force (IETF) D. Hardt, Ed. Request for Comments: 6749 Microsoft Obsoletes: 5849 October 2012 Category: Standards Track ISSN: 2070-1721 The OAuth 2.0 Authorization Framework Abstract The OAuth 2.0 authorization framework enab

HTTP Basic Authorization

在HTTP中,Basic Authorization基本认证是一种用来允许Web浏览器或其他客户端程序在请求时提供用户名和口令形式的身份凭证的一种登录验证方式. 在发送之前是以用户名追加一个冒号然后串接上口令,并将得出的结果字符串再用Base64算法编码.例如,提供的用户名是Aladdin.口令是open sesame,则拼接后的结果就是Aladdin:open sesame,然后再将其用Base64编码,得到QWxhZGRpbjpvcGVuIHNlc2FtZQ==.最终将Base64编码的字符

python——请求服务器(http请求和https请求)

一.http请求 1.http请求方式:get和post get一般用于获取/查询资源信息,在浏览器中直接输入url+请求参数点击enter之后连接成功服务器就能获取到的内容,post请求一般用于更新资源,通过form表单或者json.xml等其他形式提交给服务器端,然后等待服务器端给返回一个结果的方式(这个返回结果一般就是被修改之后的是否成功的状态,或者是修改后的最新数据table等). http请求,不论是get还是post请求,都会包含几个部分,分别是header,cookie,get会有

c# post basic 接口

string url = "http://xxxxxxxxx";//地址,请反复检查地址的准确性 string usernamePassword = "username:password"; byte[] bytes = Encoding.Default.GetBytes(usernamePassword); string str = Convert.ToBase64String(bytes); string ret = new RequestHelper().Ht

HTTP Basic Auth:BA 认证

1. 背景 梳理 BA 认证的过程,以及每一步的意义和解决的问题: 加密算法 明文输入 数字签名 潜在风险:是否可能被攻破 note: HTTP BA(基本认证)原始 BA 认证: HTTP 头部:Authorization Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 其「数字签名」默认 Base64 编码,能够被解密出来 HTTP BA 定制: HTTP 头部:Authorization Authorization: Basic QWxh

【rfc6749】机翻 The OAuth 2.0 Authorization Framework

本文禁止转载 原文地址  http://tools.ietf.org/html/rfc6749 下面内容全部是谷歌娘的翻译 = = ,写这个的目的是给自己留个备份,这样就不用每次打开谷歌娘了. ------------------------- 机翻的分割线 ---------------------------------------- OAuth的2.0授权框架 抽象 OAuth的2.0授权框架允许第三方 应用程序获取有限的访问HTTP服务,无论是在 代表的资源所有者通过编排批准互动 资源所

RobotFrameWork http/https oauth接口测试 (二)

在RobotFrameWork http/https oauth接口测试 (一)中,大致介绍了相关的概念,终于可以步入正题了~~~ 先介绍下项目背景: 公司的项目采用的授权模式是第三种resource owner password credentials密码模式,因为认证服务器和资源服务器都是公司内部的项目,所以采用了这个方式,公司的处理流程大致如下: (A)用户向客户端提供账号和密码. (B) 客户端将用户凭证以及客户端凭证信息发送到认证服务器. (C) 认证服务器校验客户端请求合法,并且用户

Web验证方式--Http Basic Authentication

Http Basic Authentication是HTTP协议中定义的Web系统中的验证方式.参考wiki 主要的实现机制如下: 1. 用户通过浏览器匿名访问web资源. 2. web服务器检测到web资源是需要已验证的用户才能访问.向浏览器返回Response(状态码401).该response会携带如下Header: WWW-Authenticate: {authentication schema} realm="{The realm of the resource}" 对于该h