使用HttpClient进行HTTP BASIC验证
1.maven项目引入HttpClient依赖,或导入jar包
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.1</version>
</dependency>
2.编写方法,输入定义的用户名、密码,返回一个httpClient对象
public static CloseableHttpClient getHttpClient(){CredentialsProvider provider = new BasicCredentialsProvider();UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(USERNAME, PASSWORD);provider.setCredentials(AuthScope.ANY, credentials);return HttpClients.custom().setDefaultCredentialsProvider(provider).build();
}
3.将返回的httpClient对象使用HttpClient发出get或post等请求
public static String createStream(String url, String name) {CloseableHttpClient httpClient = getHttpClient();HttpPost httpPost = new HttpPost(url);String s = "";List<NameValuePair> parameters = new ArrayList<NameValuePair>();parameters.add(new BasicNameValuePair("name", name));try {UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,"UTF-8");httpPost.setEntity(formEntity);CloseableHttpResponse response = httpClient.execute(httpPost);HttpEntity entity = response.getEntity();s = EntityUtils.toString(entity);System.out.println(s);httpClient.close();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return s;
}
使用HttpClient进行HTTP BASIC验证相关推荐
- HTTP的 Basic 验证
什么是HTTP Basic Authentication? 在wiki上有详细的解释: http://en.wikipedia.org/wiki/Basic_authentication_scheme ...
- HTTP Basic 验证客户端 C#实现笔记
HTTP Basic 验证客户端的原理: 把HTTP头重的ContentType设置为:application/x-www-form-urlencoded 如果HTTP头没有Authorization ...
- WebApi的调用-3.Basic验证
webapi里的特性 /// <summary>/// Basic验证 /// </summary>/// <remarks>/// /// </remark ...
- 地图不是领地尼克·格里尔的采访
Information designers and data visualization practitioners are often concerned with The Map - how it ...
- HttpClient basic authentication
2019独角兽企业重金招聘Python工程师标准>>> 1. Overview This tutorial will illustrate how to configure Basi ...
- 客官,来看看AspNetCore的身份验证吧
作者:句幽 来源:https://www.cnblogs.com/uoyo/p/13209685.html 开篇 这段时间潜水了太久,终于有时间可以更新一篇文章了. 通过本篇文章您将Get: Http ...
- HttpClient 请求 URL字符集转码问题
问题是这样的,我用eclipse发送httpclient请求如下没有问题,但是在idea中就返回400,为毛呢???excuse me? package com.vol.timingTasks;imp ...
- HttpClient 教程 (二)
转自:http://www.cnblogs.com/loveyakamoz/archive/2011/07/21/2112832.html 第二章 连接管理 HttpClient有一个对连接初始化和终 ...
- springboot-springSecurity 之 http Basic认证 (四)
引言: HTTP基础认证(BA)是一种简单的认证机制.当一个web客户端需要保护任何web资源的时候,服务器会发送一个带有401状态码(未授权)的HTTP回应,还有类似WWW-Authenticate ...
最新文章
- id 的选择器为什么要这么写 li#first?
- 算法设计与分析【第七周】贪心算法 最优装载问题
- WPF Application启动界面设置——
- 从草根到百万年薪程序员的十年风雨之路,吊打面试官系列!
- android+apk启动过程,Android Apk的运行过程(一)—— Activity启动与页面事件传递...
- PHP开发框架[国内框架]
- SpringMVC 返回值分类
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- 黑苹果 电脑关机是因为发生了问题_【电脑常识】常见的电脑误区,你中了几点?...
- error: Setup script exited with error: Unable to find vcvarsall.bat
- qgc地面站如何导入离线地图_如何下载百度地图离线包并导入OruxMaps
- Java程序编写 • 【第4章 程序:随机本周菜品;简易计算器】
- pdf文件怎么合并在一起
- [Bada开发]使用静态库
- 防saq注入_盛大在线另一分站某SQL注入
- 关于web出现此问题:index:25 Uncaught ReferenceError: delFruit is not defined at HTMLImageElement.onclick
- 120.阿克曼(Ackmann)函数A(m,n)中,m,n定义域是非负整数(m<=3,n<=10),计算如下函数值
- 眼动数据分析基础_02
- 白大脑比超级计算机还,《精灵宝可梦》图鉴376:大脑的运算速度堪比计算机——巨金怪...
- tensorflow中的ops(或者说op)的理解