Skip to content

Commit

Permalink
调整httpClient设置代理的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix committed Feb 10, 2016
1 parent cb8728d commit b1fc57a
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main/java/edu/scup/io/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ public class HttpClient {
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36";
private static final ObjectMapper objectMapper = new ObjectMapper();
public static Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 1080));
public static boolean useProxy;
private static final int socketReadTimeout = 30000;
private static final int connectTimeout = 5000;
public static final int socketReadTimeout = 30000;
public static final int connectTimeout = 5000;

public static String getResponse(String url) throws IOException {
return getResponse(url, null);
}

public static String getResponse(String url, Proxy proxy) throws IOException {
try {
HttpURLConnection conn = openConnection(url);
HttpURLConnection conn = openConnection(url, proxy);
String contentType = conn.getContentType();
String encoding = "utf-8";
if (contentType != null && contentType.indexOf("charset=") > 0) {
Expand Down Expand Up @@ -127,14 +130,18 @@ public static int downloadFile(String fileUrl, File saveTo) throws IOException,
}

public static HttpURLConnection openConnection(String url) throws URISyntaxException, IOException {
return openConnection(url, null);
}

public static HttpURLConnection openConnection(String url, Proxy proxy) throws URISyntaxException, IOException {
if (logger.isDebugEnabled()) {
if (useProxy) {
if (proxy != null) {
logger.debug("open url {} with proxy {}", url, proxy);
} else {
logger.debug("open url {}", url);
}
}
URLConnection conn = useProxy ? new URI(url).toURL().openConnection(proxy) : new URI(url).toURL().openConnection();
URLConnection conn = proxy != null ? new URI(url).toURL().openConnection(proxy) : new URI(url).toURL().openConnection();
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(socketReadTimeout);
Expand Down

0 comments on commit b1fc57a

Please sign in to comment.