In
some cases, we are not allowed to make direct http call to fetch the
content.This is due to firewall or org policy. In this case we need to
find a way to access outside server via proxy.
Here I am talking about making http call via proxy settings. In this implementation, we need few class to be imported for apache and java net:
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.utils.URIBuilder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.Date;
First step is to create url where we need to connect to access the content:
String path = "test.htm";
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("www.google.com").setPath(path);
URI targetUrl = builder.build();
Result: http://www.google.com/test.htm
Second step is to create proxy server:
HttpHost proxy = new HttpHost("proxy.server.com", "1234", "http");
Result: http://proxy.server.com:1234
Final step is to make http request call get to access content:
String content = Request.Get(targetUrl).viaProxy(proxy).connectTimeout("2000").socketTimeout("3000").execute().returnContent().asString();
Here I am talking about making http call via proxy settings. In this implementation, we need few class to be imported for apache and java net:
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.utils.URIBuilder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.Date;
First step is to create url where we need to connect to access the content:
String path = "test.htm";
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("www.google.com").setPath(path);
URI targetUrl = builder.build();
Result: http://www.google.com/test.htm
Second step is to create proxy server:
HttpHost proxy = new HttpHost("proxy.server.com", "1234", "http");
Result: http://proxy.server.com:1234
Final step is to make http request call get to access content:
String content = Request.Get(targetUrl).viaProxy(proxy).connectTimeout("2000").socketTimeout("3000").execute().returnContent().asString();
No comments:
Post a Comment