HttpRequestTest.java
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.shadowsocks.socks5;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
public class HttpRequestTest {
public static void main(String[] args) throws Exception {
final String user = "t";
final String password = "test";
Proxy proxyTest = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 10000));
// java.net.Authenticator.setDefault(new java.net.Authenticator()
// {
// private PasswordAuthentication authentication = new PasswordAuthentication(user, password.toCharArray());
//
// @Override
// protected PasswordAuthentication getPasswordAuthentication()
// {
// return authentication;
// }
// });
OkHttpClient client = new OkHttpClient.Builder().proxy(proxyTest).build();
Request request = new Request.Builder().url("http://www.baidu.com").build();
Response response = client.newCall(request).execute();
System.out.println(response.code());
System.out.println(response.body());
client.dispatcher().executorService().shutdown();
client.connectionPool().evictAll();
}
public void socks5PortTest() throws Exception{
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 10000));
HttpGet get = new HttpGet("http://www.baidu.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");
HttpClient httpClient = HttpClients.createDefault();
httpClient.execute(get);
}
}