java 如何在httpget中设置用户名/密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10004003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:12:48  来源:igfitidea点击:

how to set user name/password in httpget

javahttpclient

提问by user468587

I use Apache HttpComponents to access a web service, and don' know how to set user/password in the request, here is my code:

我使用 Apache HttpComponents 访问 Web 服务,但不知道如何在请求中设置用户/密码,这是我的代码:

URI url = new URI(query);
HttpGet httpget = new HttpGet(url);

DefaultHttpClient httpclient = new DefaultHttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("test", "test");
httpclient.getCredentialsProvider().setCredentials(new AuthScope(HOST, AuthScope.ANY_PORT), defaultcreds);

HttpResponse response = httpclient.execute(httpget);

..

..

but still it got the 401 unauthorized error.

但它仍然收到 401 未经授权的错误。

HTTP/1.1 401 Unauthorized [Server: Apache-Coyote/1.1, Pragma: No-cache, Cache-Control: no-cache, Expires: Wed, 31 Dec 1969 16:00:00 PST, WWW-Authenticate: Basic realm="MemoryRealm", Content-Type: text/html;charset=utf-8, Content-Length: 954, Date: Wed, 04 Apr 2012 02:28:49 GMT]

I m not sure if its the right way to set user/password? anyone can help? thanks.

我不确定它是否是设置用户/密码的正确方法?任何人都可以帮忙吗?谢谢。

回答by Jasonw

I think you are on the right track. Perhaps you should check your user credential as the http error responsecould probably means incorrect username/password or the user does not have the privilege to access the resources. I have the below code which I do basic http authentication and it is working fine.

我认为你在正确的轨道上。也许您应该检查您的用户凭据,因为 http错误响应可能意味着不正确的用户名/密码或用户没有访问资源的权限。我有以下代码,用于进行基本的 http 身份验证,并且工作正常。

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


public class Authentication
{

    public static void main(String[] args)
    {

        DefaultHttpClient dhttpclient = new DefaultHttpClient();

        String username = "abc";
        String password = "def";
        String host = "abc.example.com";
        String uri = "http://abc.example.com/protected";

        try
        {
            dhttpclient.getCredentialsProvider().setCredentials(new AuthScope(host, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password));
            HttpGet dhttpget = new HttpGet(uri);

            System.out.println("executing request " + dhttpget.getRequestLine());
            HttpResponse dresponse = dhttpclient.execute(dhttpget);

            System.out.println(dresponse.getStatusLine()    );
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dhttpclient.getConnectionManager().shutdown();
        }

    }

}