我是靠谱客的博主 幸福八宝粥,最近开发中收集的这篇文章主要介绍java http 用户名密码_httpclient 用户名密码认证实例,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;

import org.apache.commons.httpclient.Header;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.httpclient.UsernamePasswordCredentials;

import org.apache.commons.httpclient.auth.AuthScope;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClientUse {

public static void main(String[] args) throws HttpException, IOException {

HttpClient httpClient = new HttpClient();

//需要验证

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("guest", "guest");

httpClient.getState().setCredentials(AuthScope.ANY, creds);

//设置http头

List headers = new ArrayList ();

headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));

httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);

GetMethod method = new GetMethod("http://localhost:15672/api/exchanges/%2F/amq.direct");

method.setDoAuthentication(true);

method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,

new DefaultHttpMethodRetryHandler(3, false));

try {

int statusCode = httpClient.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {

System.out.println("Method failed code="+statusCode+": " + method.getStatusLine());

} else {

System.out.println(new String(method.getResponseBody(), "utf-8"));

}

} finally {

method.releaseConnection();

}

}

}

最后

以上就是幸福八宝粥为你收集整理的java http 用户名密码_httpclient 用户名密码认证实例的全部内容,希望文章能够帮你解决java http 用户名密码_httpclient 用户名密码认证实例所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部