相关疑难解决方法(0)

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

我试图模仿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" + …
Run Code Online (Sandbox Code Playgroud)

java http apache-commons

147
推荐指数
8
解决办法
40万
查看次数

标签 统计

apache-commons ×1

http ×1

java ×1