使用HttpClient在Java中进行Http基本身份验证?

Leg*_*end 147 java http apache-commons

我试图模仿Java中这个curl命令的功能:

curl --basic --user username:password -d "" http://ipaddress/test/login
Run Code Online (Sandbox Code Playgroud)

我使用Commons HttpClient 3.0编写了以下内容但不知何故最终500 Internal Server Error从服务器获取了一个.有人能告诉我,如果我做错了吗?

public class HttpBasicAuth {

    private static final String ENCODING = "UTF-8";

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {

            HttpClient client = new HttpClient();

            client.getState().setCredentials(
                    new AuthScope("ipaddress", 443, "realm"),
                    new UsernamePasswordCredentials("test1", "test1")
                    );

            PostMethod post = new PostMethod(
                    "http://address/test/login");

            post.setDoAuthentication( true );

            try {
                int status = client.executeMethod( post );
                System.out.println(status + "\n" + post.getResponseBodyAsString());
            } finally {
                // release any connection resources used by the method
                post.releaseConnection();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   } 
Run Code Online (Sandbox Code Playgroud)

我后来尝试了Commons HttpClient 4.0.1,但仍然出现同样的错误:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;


public class HttpBasicAuth {

    private static final String ENCODING = "UTF-8";

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();

            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
                    new UsernamePasswordCredentials("test1", "test1"));

            HttpPost httppost = new HttpPost("http://host:post/test/login");

            System.out.println("executing request " + httppost.getRequestLine());
            HttpResponse response;
            response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            if (entity != null) {
                entity.consumeContent();
            }

            httpclient.getConnectionManager().shutdown();  
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
Run Code Online (Sandbox Code Playgroud)

Buh*_*ndi 174

Have you tried this (using HttpClient version 4):

String encoding = Base64Encoder.encode(user + ":" + pwd);
HttpPost httpPost = new HttpPost("http://host:post/test/login");
httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + encoding);

System.out.println("executing request " + httpPost.getRequestLine());
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
Run Code Online (Sandbox Code Playgroud)

  • 最好从Java 8开始使用`java.util.Base64`:`Base64.getEncoder().encodeToString(("test1:test1").getBytes());` (57认同)

Leg*_*end 109

好的,这个有用.万一有人想要它,这是适合我的版本:)

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;


public class HttpBasicAuth {

    public static void main(String[] args) {

        try {
            URL url = new URL ("http://ip:port/login");
            String encoding = Base64.getEncoder().encodeToString(("test1:test1").getBytes(?"UTF??-8"?));

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty  ("Authorization", "Basic " + encoding);
            InputStream content = (InputStream)connection.getInputStream();
            BufferedReader in   = 
                new BufferedReader (new InputStreamReader (content));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

  • 这不回答这个问题.问题是关于使用HttpClient,这个答案不使用HttpClient. (21认同)
  • 如果您使用的是Java 8,则可以使用java.util.Base64. (9认同)
  • 找不到`Base64Encoder`.乔纳斯,你可以请完整的罐子吗?还有什么是`Base64Encoder`的完全限定类名? (4认同)
  • 这是java.util.Base64的行`String encoding = Base64.getEncoder().encodeToString("test1:test1".getBytes("utf-8"));` (4认同)

Mar*_*rio 16

这是上面接受的答案中的代码,对Base64编码进行了一些更改.下面的代码编译.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.codec.binary.Base64;


public class HttpBasicAuth {

    public static void main(String[] args) {

        try {
            URL url = new URL ("http://ip:port/login");

            Base64 b = new Base64();
            String encoding = b.encodeAsString(new String("test1:test1").getBytes());

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty  ("Authorization", "Basic " + encoding);
            InputStream content = (InputStream)connection.getInputStream();
            BufferedReader in   = 
                new BufferedReader (new InputStreamReader (content));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } 
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 13

一个小小的更新 - 希望对某人有用 - 它在我的项目中对我有用:

  • 我使用来自Robert Harder的漂亮的Public Domain类Base64.java(感谢Robert - 代码可用:Base64 - 下载并将其放入您的包中).

  • 并通过身份验证下载文件(图像,文档等)并写入本地磁盘

例:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpBasicAuth {

public static void downloadFileWithAuth(String urlStr, String user, String pass, String outFilePath) {
    try {
        // URL url = new URL ("http://ip:port/download_url");
        URL url = new URL(urlStr);
        String authStr = user + ":" + pass;
        String authEncoded = Base64.encodeBytes(authStr.getBytes());

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.setRequestProperty("Authorization", "Basic " + authEncoded);

        File file = new File(outFilePath);
        InputStream in = (InputStream) connection.getInputStream();
        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        for (int b; (b = in.read()) != -1;) {
            out.write(b);
        }
        out.close();
        in.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

  • 我得到`方法encodeBytes(byte [])未定义类型Base64` (2认同)
  • 在java 8中,您可以使用:import java.util.Base64; (2认同)

Bru*_*uno 7

以下是几点:

  • 您可以考虑升级到HttpClient 4(一般来说,如果可以,我认为版本3仍然没有得到积极支持).

  • 500状态代码是服务器错误,因此查看服务器所说的内容(您正在打印的响应正文中的任何线索?)可能很有用.虽然它可能是由您的客户端引起的,但服务器不应该以这种方式失败(如果请求不正确,则4xx错误代码会更合适).

  • 我认为setDoAuthentication(true)是默认的(不确定).可能有用的尝试是先发制人认证更好:

    client.getParams().setAuthenticationPreemptive(true);
    
    Run Code Online (Sandbox Code Playgroud)

否则,curl -d ""你在Java和你正在做的事情之间的主要区别在于,除此之外Content-Length: 0,curl还会发送Content-Type: application/x-www-form-urlencoded.请注意,就设计而言,您可能应该发送一个包含您POST请求的实体.


man*_*noj 7

使用 Header 数组时

String auth = Base64.getEncoder().encodeToString(("test1:test1").getBytes());
Header[] headers = {
    new BasicHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()),
    new BasicHeader("Authorization", "Basic " +auth)
};
Run Code Online (Sandbox Code Playgroud)


rjd*_*olb 6

使用 HTTP POST 登录而不执行任何 Base64 特定调用的简单方法是使用 HTTPClient BasicCredentialsProvider

import java.io.IOException;
import static java.lang.System.out;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;

//code
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, password);
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();

HttpResponse response = client.execute(new HttpPost("http://address/test/login"));//Replace HttpPost with HttpGet if you need to perform a GET to login
int statusCode = response.getStatusLine().getStatusCode();
out.println("Response Code :"+ statusCode);
Run Code Online (Sandbox Code Playgroud)


che*_*976 5

感谢上面的所有答案,但对我来说,我找不到Base64Encoder类,所以无论如何我都要整理好自己的方式.

public static void main(String[] args) {
    try {
        DefaultHttpClient Client = new DefaultHttpClient();

        HttpGet httpGet = new HttpGet("https://httpbin.org/basic-auth/user/passwd");
        String encoding = DatatypeConverter.printBase64Binary("user:passwd".getBytes("UTF-8"));
        httpGet.setHeader("Authorization", "Basic " + encoding);

        HttpResponse response = Client.execute(httpGet);

        System.out.println("response = " + response);

        BufferedReader breader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder responseString = new StringBuilder();
        String line = "";
        while ((line = breader.readLine()) != null) {
            responseString.append(line);
        }
        breader.close();
        String repsonseStr = responseString.toString();

        System.out.println("repsonseStr = " + repsonseStr);

    } catch (IOException e) {
        e.printStackTrace();
    }

}
Run Code Online (Sandbox Code Playgroud)

还有一件事,我也试过了

Base64.encodeBase64String("user:passwd".getBytes());
Run Code Online (Sandbox Code Playgroud)

它不起作用,因为它返回一个几乎相同的字符串

DatatypeConverter.printBase64Binary()
Run Code Online (Sandbox Code Playgroud)

但以"\ r \n"结尾,则服务器将返回"错误请求".

以下代码也正常工作,实际上我首先解决了这个问题,但由于某种原因,它在某些云环境中不起作用(sae.sina.com.cn如果你想知道,它是一个中国的云服务).所以必须使用http标头而不是HttpClient凭据.

public static void main(String[] args) {
    try {
        DefaultHttpClient Client = new DefaultHttpClient();
        Client.getCredentialsProvider().setCredentials(
                AuthScope.ANY,
                new UsernamePasswordCredentials("user", "passwd")
        );

        HttpGet httpGet = new HttpGet("https://httpbin.org/basic-auth/user/passwd");
        HttpResponse response = Client.execute(httpGet);

        System.out.println("response = " + response);

        BufferedReader breader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder responseString = new StringBuilder();
        String line = "";
        while ((line = breader.readLine()) != null) {
            responseString.append(line);
        }
        breader.close();
        String responseStr = responseString.toString();
        System.out.println("responseStr = " + responseStr);

    } catch (IOException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)