Created
January 13, 2015 15:15
-
-
Save mikelax/d6df7c6059a2d5d5da9a to your computer and use it in GitHub Desktop.
Sample Vhost GET Using HttpClient 4.x
This file contains 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.ByteArrayOutputStream; | |
import java.io.IOException; | |
import org.apache.http.HttpStatus; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import com.google.common.io.ByteStreams; | |
public class TestHttpClient { | |
public static void main (String args[]) { | |
CloseableHttpClient client = HttpClients.createSystem(); | |
HttpHost target = new HttpHost("www.google.com"); | |
HttpGet get = new HttpGet("http://www.google.ru/"); | |
try { | |
final HttpResponse response = client.execute(target, get); | |
int statusCode = response.getStatusLine().getStatusCode(); | |
if(HttpStatus.SC_OK == statusCode && response.getEntity() != null) { | |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | |
ByteStreams.copy(response.getEntity().getContent(), bytes); | |
System.out.println(bytes.toString()); | |
} | |
} catch (ClientProtocolException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment