httpClient4.5.3 代理设置账号

来源:互联网 发布:淘宝删除宝贝怎么恢复 编辑:程序博客网 时间:2024/05/20 23:39
public static void main(String[] args) {getHtml("http://news.qq.com",null);}public static CloseableHttpResponse getHtml(String url,String cookie) {CredentialsProvider credsProvider = new BasicCredentialsProvider();credsProvider.setCredentials(    new AuthScope("202.194.232.132",10010 ),     new UsernamePasswordCredentials("qf", "qf3.1415"));CookieStore cookieStore = null;CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).setDefaultCookieStore(cookieStore).build();HttpGet hp = new HttpGet(url);        HttpHost proxy = new HttpHost("202.194.232.132", 10010);        RequestConfig config = RequestConfig.custom()        .setSocketTimeout(10000).setConnectTimeout(10000).setProxy(proxy).build();        CloseableHttpResponse response = null;System.out.println("开始  请求 !!");try {hp.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0");hp.addHeader(HttpHeaders.ACCEPT,"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");hp.addHeader(HttpHeaders.ACCEPT_LANGUAGE,"zh-CN,zh;q=0.8");if(cookie!=null)hp.addHeader("cookie",cookie);hp.setConfig(config);response = httpclient.execute(hp);            HttpEntity entity = response.getEntity();String html = EntityUtils.toString(entity,"UTF-8");            System.out.println(html);            EntityUtils.consume(entity);            response.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {//hp.releaseConnection();//try {////httpclient.close();//} catch (IOException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}}return response;}

 
原创粉丝点击