2017-02-20 70 views
0

我嘗試使用Java中的NTLM Autentication發送HttpRest調用。我嘗試使用org.apache.http - Lib。使用HttpClient通過匿名身份驗證發送Post請求並不是一個大問題。 但我有WindowsAuthentication一些麻煩。我甚至試圖使用我自己的Windows憑據CredentialProvider(作爲妥協,我不喜歡那個),但我沒有成功。 有沒有簡單的方法使用NTLM身份驗證發送來自Java代碼的發佈請求? 有沒有其他的lib更適合我的需求?Http post requests unsing NTLM Authentication(java)

THX - 馬庫斯

回答

2

我仍然不知道爲什麼從https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html有關NTLM身份驗證的數獨沒有爲我工作。作爲http://www.baeldung.com/httpclient-post-http-request

描述 我終於解決了我的問題,做類似於基本身份驗證的文件,現在看起來是這樣的:

... 
CredentialsProvider credsProvider = new BasicCredentialsProvider(); 
     credsProvider.setCredentials(AuthScope.ANY, 
       new NTCredentials("username", "passwd", hostname, "domain.at")); 


HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build(); 

HttpPost post = new HttpPost("http://www.example.com")); 

StringEntity input = new StringEntity(bodyAsString, HTTP.UTF_8); 
input.setContentType("application/json"); 
input.setContentEncoding("UTF-8"); 
post.setEntity(input); 

post.setHeader("Accept", "application/json"); 
post.setHeader("Content-type", "application/json"); 


HttpResponse response = client.execute(post); 
...