java - HTTP 错误 401.2 尝试使用 DefaultHttpClient 调用具有用户名和密码的 URL 时

标签 java android http

这是我的代码:

private DefaultHttpClient httpClient;

public HttpAPIRequest(String authScope, int port, Credentials credentials){
    httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(authScope, port), 
            new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword()));
}

public String request(String apiUrl) throws ClientProtocolException, IllegalStateException, IOException{
    
    HttpGet httpGet = new HttpGet(apiUrl);
    HttpResponse response = httpClient.execute(httpGet);
    return getResponseBody(response);
}

这是我在响应体中得到的错误

HTTP Error 401.2 - Unauthorized: Access is denied due to server configuration.Internet Information Services (IIS)

You do not have permission to view this directory or page using the credentials that you supplied because your Web browser is sending a WWW-Authenticate header field that the Web server is not configured to accept.

我该如何解决这个问题?

最佳答案

有几种方法可以对 URL 进行身份验证,您收到的错误表明您正在发送 WWW-Authenticate header ,但网络服务器未配置为接受它。您要验证哪个 URL?当您尝试使用常规网络浏览器访问它时,它看起来像什么?

很有可能,它配置为“基于表单的身份验证”,这意味着上面有一个带有 Web 表单的普通网页,您可能可以通过发布表单参数等来针对它编写脚本。

关于java - HTTP 错误 401.2 尝试使用 DefaultHttpClient 调用具有用户名和密码的 URL 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8368424/

相关文章:

java - 从 BST 中删除节点

java - 如何在生产环境中运行 Spring Boot 可执行 jar?

android - android 上的 FFmpeg 在 avcodec_decode_video2 函数中崩溃

android - 在 3.0 之前的设备上引用 android.R.id.home

Java:如何创建 HTTP 浏览 session

PHP 不能在带有 http_build_query() 的数组中使用 'not'

java - 如何在android studio中生成多值二维码

java - 执行 phonegap 命令 cordova build 时出错

android - 仅在父 fragment 中的操作栏中显示搜索 View

c# - 如何在 ASP.NET MVC 中发布 List<T> 并使模型正常工作?