Apache HttpComponents 通过代理发送HTTP请求
package org.apache.http.examples.client; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; /**
* A simple example that uses HttpClient to execute an HTTP request
* over a secure connection tunneled through an authenticating proxy.
*/
public class ClientProxyAuthentication { public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("username", "password")); HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
HttpHost proxy = new HttpHost("localhost", 8080); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); HttpGet httpget = new HttpGet("/"); System.out.println("executing request: " + httpget.getRequestLine());
System.out.println("via proxy: " + proxy);
System.out.println("to target: " + targetHost); HttpResponse response = httpclient.execute(targetHost, httpget);
HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity); } finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
HttpHost proxy = new HttpHost("localhost", 8888);设置代理的地址
HttpResponse response = httpclient.execute(proxy,httppost);第一个参数为代理,第二个参数为真是地址
Apache HttpComponents 通过代理发送HTTP请求的更多相关文章
- org.apache.httpcomponents httpclient 发起HTTP JSON请求
1. pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactI ...
- Java使用Apache的HttpClient组件发送https请求
如果我们直接通过普通的方式对https的链接发送请求,会报一个如下的错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Va ...
- Java 使用代理发送Http请求 (将Http请求代理Https请求)
package com.test.porxy; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...
- HttpClient服务端发送http请求
本来以为对跨域问题的处理已经比较熟练了.可以通过jsonp.document.domain+iframe.window.name.window.postMessage.服务器上设置代理页面来解决.但还 ...
- Apache 代理(Proxy) 转发请求
代理分为:正向代理(Foward Proxy)和反向代理(Reverse Proxy) 1.正向代理(Foward Proxy) 正向代理(Foward Proxy)用于代理内部网络对Internet ...
- JAVA使用apache http组件发送POST请求
在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...
- JAVA中使用Apache HttpComponents Client的进行GET/POST请求使用案例
一.简述需求 平时我们需要在JAVA中进行GET.POST.PUT.DELETE等请求时,使用第三方jar包会比较简单.常用的工具包有: 1.https://github.com/kevinsawic ...
- 通过 Apache Commons HttpClient 发送 HTTPS 请求
1.通过 HTTPS 发送 POST 请求: 2.HTTPS 安全协议采用 TLSv1.2: 3. 使用代理(Proxy)进行 HTTPS 访问: 4.指定 Content-Type 为:applic ...
- 使用URLConnection发送http请求实现简单爬虫(可以配置代理)
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import jav ...
随机推荐
- MySQL-五种日志(查询日志、慢查询日志、更新日志、二进制日志、错误日志)、备份及主从复制配置
开启查询日志: 配置文件my.cnf: log=/usr/local/mysql/var/log.log 开启慢查询: 配置文件my.cnf: log-slow-queries=/usr/local/ ...
- HTML:调用静态页面html 的几种方法
今天做办公用品管理系统时,发现需要用到在一个静态页面html 中调用多个静态页面html的内容.查找资料总结了以下一些方法: 一.iframe引入的方法 代码如下: <!-- 部门--> ...
- 【LeetCode】31. Next Permutation (2 solutions)
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- 【LeetCode】141. Linked List Cycle (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- 解释-DNS,A记录,CNAME记录,域名转向,SRV记录,TTL值,泛域名与泛解析,域名绑定
http://www.lihongye.net/post/dns.html DNS DNS,Domain Name System或者Domain Name Service(域名系统或者域名服务).域名 ...
- java JAXB + STAX(是一种针对XML的流式拉分析API)读取xml
JDK1.5需要添加jar包,1.6以后就不需要了<dependency> <groupId>stax</groupId> <artifactId>st ...
- 于C#控制台传递参数和接收参数
前言: 写了这么久程序,今天才知道的一个基础知识点,就是程序入口 static void Main(string[] args) 里的args参数是什么意思 ?惭愧... 需求: 点击一个button ...
- Unix环境高级编程(十一)线程
一个进程在同一时刻只能做一件事情,线程可以把程序设计成在同一时刻能够做多件事情,每个线程处理各自独立的任务.线程包括了表示进程内执行环境必需的信息,包括进程中标识线程的线程ID.一组寄存器值.栈.调度 ...
- GO1.6语言学习笔记2-安装配置及代码组织
一.关于GO开发环境的安装和配置 在linux环境中安装编译好的go安装包,参考官方指南的步骤一步步走下来就可以了.需要注意的是以下几个环境变量的配置: GOROOT - ...
- python 例程的一个好例子
用例程来写一个求平均值的算法 #!/usr/local/python/bin/python3 def FunCore(): total=0 counter=0 average=None tmp=yie ...