zoukankan      html  css  js  c++  java
  • 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 <Header> headers = new ArrayList <Header>();
    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();
    }
    }
    }

  • 相关阅读:
    关于基于.net的WEB程序开发所需要的一些技术归纳
    技术的学习方法探索之一
    生活,就是让程序为人们服务!
    js滑动提示效果
    radio判断是否为空
    JS清除网页历史记录,屏蔽后退按钮
    多表查询存储过程
    IP地址转化为数字,charindex ,SUBSTRING
    c# byte转化为string
    获得IP地址中文
  • 原文地址:https://www.cnblogs.com/jing1617/p/6365744.html
Copyright © 2011-2022 走看看