import java.net.URI;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; public class Http { // 如何通过一个代理发送一个HTTP请求。
public void action1() throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpHost target = new HttpHost("ip1", 8080, "https");
HttpHost proxy = new HttpHost("ip2", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpGet request = new HttpGet("/");
request.setConfig(config); System.out.println("Executing request " + request.getRequestLine()
+ " to " + target + " via " + proxy); httpclient.execute(target, request);
} finally {
httpclient.close();
}
} // 得到cookies。
public void action2() throws Exception {
BasicCookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore).build();
try {
// HttpGet httpget = new HttpGet("https://someportal/?username=username&password=password");
// CloseableHttpResponse response = httpclient.execute(httpget);
HttpUriRequest login = RequestBuilder.post()
.setUri(new URI("https://www.baidu.com/"))
.addParameter("username", "username")
.addParameter("password", "password").build();
httpclient.execute(login); System.out.println("请求所得的 cookies:");
List<Cookie> cookies = cookieStore.getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
} } finally {
httpclient.close();
} }
// 设置超时。
public void action3() throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(1000)
.setConnectTimeout(1000)
.build();
HttpGet httpget1 = new HttpGet("https://www.baidu.com");
httpget1.setConfig(requestConfig);
httpclient.execute(httpget1);
}
}

HttpClient设置代理,超时,以及得到cookies的更多相关文章

  1. 通过httpClient设置代理Ip

    背景: 我们有个车管系统,需要定期的去查询车辆的违章,之前一直是调第三方接口去查,后面发现数据不准确(和深圳交警查的对不上),问题比较多.于是想干脆直接从深圳交警上查,那不就不会出问题了吗,但是问题又 ...

  2. HttpClient 设置代理方式

    HttpClient httpClient = new HttpClient(); //设置代理服务器的ip地址和端口 httpClient.getHostConfiguration().setPro ...

  3. 转 HttpClient 设置连接超时时间

    要: HttpClient 4.5版本升级后,设置超时时间的API又有新的变化,请大家关注. HttpClient升级到4.5版本后,API有很多变化,HttpClient 4之后,API一直没有太稳 ...

  4. HttpClient设置连接超时时间

    https://www.cnblogs.com/winner-0715/p/7087591.html 使用HttpClient,一般都需要设置连接超时时间和获取数据超时时间.这两个参数很重要,目的是为 ...

  5. 解决httpclient设置代理ip之后请求无响应的问题

    httpclient这个工具类对于大家来说应该都不陌生吧,最近在使用过程中出现了碰到一个棘手的问题,当请求的接口地址由http变成https之后,程序执行到 httpClient.execute(ht ...

  6. java HttpClient设置代理

    HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCreden ...

  7. 论述Android通过HttpURLConnection与HttpClient联网代理网关设置

    Android联网主要使用HttpURLConneciton和HttpClient进行联网,在手机联网的时候,我们优先选择wifi网络,其次在选择移动网络,这里所述移动网络主要指cmwap. 大家都知 ...

  8. HttpClient设置超时(转)

    HttpClient  4.5版本设置连接超时时间-CloseableHttpClient设置Timeout(区别于4.3.2) HttpClient升级到4.5版本后,API有很多变化,HttpCl ...

  9. HttpClient代码设置代理

    由于对接faceBook接口,本地测试时候要设置代理才能调试. (http和https通用) public SSLContext createIgnoreVerifySSL() throws NoSu ...

随机推荐

  1. ldconfig报错 :libstdc++.so.6.0.18-gdb.py不是一个elf文件

    今天安装wxWidgets,输入ldconfig竟然提示 /usr/lib64/libstdc++.so.6.0.18-gdb.py 不是一个elf文件,开头魔数错误 摸不着头脑,上网搜了一下,有说是 ...

  2. ACM1994

    /* Problem Description 为自行解决学费,chx勤工俭学收入10000元以1年定期存入银行,年利率为3.7% .利率按年计算,表示100元存1年的利息为3.7元.实际上有时提前有时 ...

  3. Hadoop2.2.0(yarn)编译部署手册

    Created on 2014-3-30URL : http://www.cnblogs.com/zhxfl/p/3633919.html @author: zhxfl   Hadoop-2.2编译 ...

  4. linux网络编程笔记——TCP

    1.TCP和UDP TCP是长连接像持续的打电话,UDP是短消息更像是发短信.TCP需要消耗相对较多的资源,但是传输质量有保障,UDP本身是不会考虑传输质量的问题. 2.网络传输内容 我习惯的做法是直 ...

  5. append some buttons to the standard datagrid pager bar

    <script type="text/javascript">  $(function(){   var pager = $('#dg').datagrid('getP ...

  6. POJ 3660 Cow Contest (Floyd)

    http://poj.org/problem?id=3660 题目大意:n头牛两两比赛经过m场比赛后能判断名次的有几头可转 化为路径问题,用Floyd将能够到达的路径标记为1,如果一个点能 够到达剩余 ...

  7. UIImageView旋转任意角度

    -(UIImageView *) makeRotation:(UIImageView *)image speedX:(float)X speedY:(float)Y { //    头文件中需要定义  ...

  8. hdu4614Vases and Flowers(线段树,段设置,更新时范围的右边值为变量)

    Problem Description Alice is so popular that she can receive many flowers everyday. She has N vases ...

  9. 【Linux常用工具】02. 创建启动定时任务工具cron

    一. cron 1. cron是一个守护程序,它提供定时器的功能,让用户在特定的时间得以执行默认的指令或程序.只要用户会编辑定时器的设置文件,就可以使用定时器的功能. 定时器文件格式: 2. cron ...

  10. CodeForces 706B Interesting drink (二分查找)

    题意:给定 n 个数,然后有 m 个询问,每个询问一个数,问你小于等于这个数的数有多少个. 析:其实很简单么,先排序,然后十分查找,so easy. 代码如下: #pragma comment(lin ...