Created
February 12, 2014 04:49
-
-
Save yusuke2255/8950210 to your computer and use it in GitHub Desktop.
Java HttpClient4.3でProxy(https)を使うテスト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.Header; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.config.RequestConfig; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.HttpClientBuilder; | |
import org.apache.http.message.BasicHeader; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.util.EntityUtils; | |
public class HttpClientHttpsProxyTest { | |
public static void main(String[] args) { | |
try { | |
init(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static String domain = "https://hoge.com/"; | |
private static final int socketTimeout = 3000; | |
private static final int connectTimeout = 3000; | |
private static final String userAgent = "httpClient4.3-test"; | |
private static void init() throws IOException { | |
System.out.println("===== HTTP POST Start ====="); | |
HttpHost proxy = new HttpHost("127.0.0.1", 8081); | |
RequestConfig config = RequestConfig.custom() | |
.setProxy(proxy) | |
.setConnectTimeout(connectTimeout) | |
.setSocketTimeout(socketTimeout) | |
.build(); | |
List<Header> httpHeaders = new ArrayList<>(); | |
httpHeaders.add(new BasicHeader("User-Agent", userAgent)); | |
httpHeaders.add(new BasicHeader("Accept-Charset", | |
StandardCharsets.UTF_8.toString())); | |
HttpClient httpClient = HttpClientBuilder.create() | |
.setDefaultRequestConfig(config) | |
.setDefaultHeaders(httpHeaders) | |
.build(); | |
HttpPost postMethod = new HttpPost(domain + "api/fuga"); | |
List<NameValuePair> params = new ArrayList<>(); | |
params.add(new BasicNameValuePair("param1", "parameter-1")); | |
params.add(new BasicNameValuePair("param2", "parameter-2")); | |
postMethod.setEntity(new UrlEncodedFormEntity(params)); | |
HttpResponse response = httpClient.execute(postMethod); | |
int responseCode = response.getStatusLine().getStatusCode(); | |
String responseEntity = EntityUtils.toString(response.getEntity(),StandardCharsets.UTF_8.toString()); | |
System.out.println("responseCode : " + responseCode); | |
System.out.println("responseEntity : " + responseEntity); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>test</groupId> | |
<artifactId>test</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.httpcomponents</groupId> | |
<artifactId>httpclient</artifactId> | |
<version>4.3.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment