Apache HttpClient(4.1和更新版本):如何进行基本身份验证?(Apache HttpClient (4.1 and newer): how to do basic authentication?)
如何为httpClient库的默认客户端添加基本身份验证? 我见过使用
client.getCredentialProvider()
例子,但我认为所有这些方法都是针对库版本4.0.1或3.x。 有没有一个如何做到这一点的新例子? 非常感谢。How do I add basic authentication for the default client of the httpClient library? I have seen examples where they use
client.getCredentialProvider()
, however I think all of this methods are for library version 4.0.1 or 3.x. Is there a new example of how to do this? Thanks a lot.
最满意答案
我们使用
HttpClient
进行基本身份验证,但我们不使用CredentialProvider
。 代码如下:HttpClient client = factory.getHttpClient(); //or any method to get a client instance Credentials credentials = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(AuthScope.ANY, credentials);
更新:在评论中指出,
HttpClient.getState()
方法在API的3.x版本中可用。 但是, 较新版本的API不支持该方法。We do basic authentication with
HttpClient
, but we do not useCredentialProvider
. Here's the code:HttpClient client = factory.getHttpClient(); //or any method to get a client instance Credentials credentials = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(AuthScope.ANY, credentials);
UPDATE: A stated in the comments, the
HttpClient.getState()
methos is available in version 3.x of the API. However, newer versions of the API doesn't support that method.
相关问答
更多-
Apache HttpClient(4.1和更新版本):如何进行基本身份验证?(Apache HttpClient (4.1 and newer): how to do basic authentication?)[2023-02-04]
我们使用HttpClient进行基本身份验证,但我们不使用CredentialProvider 。 代码如下: HttpClient client = factory.getHttpClient(); //or any method to get a client instance Credentials credentials = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(Au ... -
在不经过每一次上下文的情况下都很难做到这一点,但是您可以通过使用请求拦截器来执行此操作。 这是我们使用的一些代码(从他们的JIRA,iirc找到): // Pre-emptive authentication to speed things up BasicHttpContext localContext = new BasicHttpContext(); BasicScheme basicAuth = new BasicScheme(); localContext.setAttribute("preem ...
-
你试过了(使用HttpClient版本4) String encoding = Base64Encoder.encode ("test1:test1"); HttpPost httppost = new HttpPost("http://host:post/test/login"); httppost.setHeader("Authorization", "Basic " + encoding); System.out.println("executing request " + httppost.get ...
-
如何在多线程HTTPClient环境中使用基本身份验证?(Howto use basic authentication in multi threaded HTTPClient environment?)[2022-03-31]
我最近遇到了同样的问题。 有这个记录的问题: https : //issues.apache.org/jira/browse/HTTPCLIENT-1168你有两种选择: 为每个线程或请求创建一个HttpContext 使用SyncBasicHttpContext这是一个线程安全的实现 I ran into the same issue recently. There's this logged issue: https://issues.apache.org/jira/browse/HTTPCLIENT- ... -
apache的基本身份验证(basic authentication on apache)[2022-05-04]
该指令应该是:没有* 。 Location指令自动应用于该URL及其下的所有内容( /app/secure/myServlet , /app/secure/more/otherServlet等)。 如果你想在该位置进行模式匹配,你应该使用LocationMatch ,但我认为在你的情况下它是不必要的。 请注意, Location字符串必须是从域后面的斜杠开始的整个路径。 因此,如果您的域名是http://example.com/app/secure/ ... -
如何配置HttpClient进行基本身份验证?(How can a HttpClient be configured for basic - authentification?)[2022-08-08]
嗯,它可能是这样的:(注意,没有测试 - 这只是一系列随机的想法:))我刚刚尝试了手动http头变种,它应该与Base64.Default标志一起使用: requestLogin.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(), Base64.DEFAULT)); I just tried the manual http header variant, it should work with Base64.Default fl ...Afaik,如果支持http-basic-auth,就像 user:password@server:port/path 也应该工作。 你可以看看它是否适用于浏览器。 如果Confluence不支持基本身份验证,请使用firebug查找登录表单的操作(例如路径,例如/dologin.action ),方法( POST )和用户/密码字段的名称。 有了这些信息,您可以创建如下请求: HttpPost httpPost = new HttpPost(fullFormActionUrlWithServerAnd ...我怀疑你没有正确初始化auth缓存。 请试试这个。 HttpHost proxy = new HttpHost("proxy", 8080); BasicScheme proxyAuth = new BasicScheme(); // Make client believe the challenge came form a proxy proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=default")); ...Java - 使用Apache httpclient进行HTTPS基本身份验证(Java - HTTPS Basic authentification with Apache httpclient)[2022-11-20]
解决了! 错误来自服务器端 :我的回复没有包含任何标题.... httpclient似乎喜欢来自服务器的良好响应。 对于浏览器或curl来说,情况并非如此: 他们可以接收垃圾,显示它们会 ! Solved! The error was from the server side : my response did not include any headers.... httpclient seems to like well made responses from a server. That's not ...相关文章
更多- Apache Lucene 4.1 和 Solr 4.1 发布
- httpclient get请求
- apache-solr-4.0.0-BETA&IKAnalyzer4.1和apache-solr-4.0.0&IKAnalyzer4.2
- httpclient依懒包官网下载及httpclient maven依懒包获取
- httpclient post 请求
- httpClient快速入门
- HttpClient DELETE请求示例
- HttpClient CacheConfig缓存处理示例
- HttpClient PUT请求示例
- httpclient 带参数 get 请求
最新问答
更多- 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
- 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
- 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
- ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
- Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
- 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
- 功能包装避免重复(wrap of functions avoid duplicating)
- Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
- 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
- VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
- 大学电脑四级对初学者来说要多久能过
- 特殊字符删除?(Special characters remove?)
- Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
- 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
- Javaweb开发,技术路线是什么?该怎么写?
- JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
- 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
- Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
- 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
- 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
- “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
- maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
- 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
- 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
- java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
- 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
- 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
- 嵌套的模板可能性(Nested template possibilities)
- 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
- 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)