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

I am trying to mimic the functionality of this curl command in Java:

curl --basic --user username:password -d "" http://ipaddress/test/login

I wrote the following using Commons HttpClient 3.0 but somehow ended up getting an 500 Internal Server Error from the server. Can someone tell me if I'm doing anything wrong?

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();

}

}

}

And I later tried a Commons HttpClient 4.0.1 but still the same error:

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();

}

}

解决方案

Ok so this one works. Just in case anybody wants it, here's the version that works for me :)

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();

}

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值