我正在尝试创建一个应用程序,该应用程序使用用户名和密码连接到使用NTLM身份验证的网页(不是我的,因此我无法更改身份验证方法)并返回页面的来源我稍后会用它来提取我需要的信息。
我的问题在于身份验证步骤。使用HttpUrlConnection返回错误401-Unauthorized。 我正在使用jcifs。已经将它添加到build.gradle文件
compile group: 'jcifs', name: 'jcifs', version: '1.3.17'
来自此处的代码:
NTLM Authentication with HttpURLConnection
类声明之前的导入行
import org.apache.http.impl.auth.NTLMEngineException;
由于我怀疑httpclient已被弃用
,因此无法正常工作我的代码示例:
public class DataFetcher extends AsyncTask<String, Void, String>{
protected String doInBackground(String... urls){
jcifs.Config.registerSmbURLHandler();
URL url;
HttpURLConnection connection = null;
String result = "" ;
try{
url = new URL(urls[0]);
Log.i("URL Connected", "Done");
connection = (HttpURLConnection) url.openConnection();
Log.i("Connection Established","Done");
InputStream inputStream;
int status = connection.getResponseCode();
if(status != HttpURLConnection.HTTP_OK){
inputStream = connection.getErrorStream();
}else {
inputStream = connection.getInputStream();
}
Log.i("Input Stream made","Done");
InputStreamReader reader = new InputStreamReader(inputStream);
Log.i("Input Stream Reader","Done");
int data = reader.read();
while(data!=-1){
result += (char) data;
data = reader.read();
}
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
Log.i("Done",result);
Log.i("Response",""+connection.getHeaderFields());
return result;
}
}
public void fetchData(){
String url = "http://DOMAIN%5user:password@server/page.aspx";
DataFetcher fetcher = new DataFetcher();
fetcher.execute(url);
}
我检查了大多数(如果不是全部)我发现的所有链接,但是大多数链接使用HttpClient(不推荐使用)和apache客户端(也不推荐使用)
答案 0 :(得分:-1)
我正在积极处理同样的问题,但我所采用的解决方法涉及针对API 22.当Google在2018年8月开始强制执行API 23时,这显然不会取得成功。让你走吧