I'm trying to make a simple GET request:
https://www.google.com/search?q=stuff
I've used the Apache Http Lybrary:
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class INRIX {
private final CloseableHttpClient httpClient = HttpClients.createDefault();
public static void main(String[] args) {
INRIX inrix = new INRIX();
try {
inrix.peticionGet();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inrix.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void close() throws IOException {
httpClient.close();
}
private void peticionGet() throws Exception {
HttpGet getRequest = new HttpGet("https://www.google.com/search?q=stuff");
// Request Headers
getRequest.addHeader("custom-key", "stuff");
getRequest.addHeader(HttpHeaders.USER_AGENT, "Googlebot");
try (CloseableHttpResponse response = httpClient.execute(getRequest)) {
// Get HttpResponse Status
System.out.println(response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
Header headers = entity.getContentType();
System.out.println(headers);
if (entity != null) {
// return it as a String
String result = EntityUtils.toString(entity);
System.out.println(result);
}
} catch (ClientProtocolException e) {
System.out.println("ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
}
}
}
And HttpURLConnection:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class INRIXURL {
public static void main(String[] args) {
INRIXURL inrix = new INRIXURL();
inrix.peticionGet();
}
public void peticionGet() {
String url = "https://www.google.com/search?q=stuff";
HttpURLConnection httpClient;
try {
httpClient = (HttpURLConnection) new URL(url).openConnection();
// optional default is GET
httpClient.setRequestMethod("GET");
// add request header
httpClient.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = httpClient.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
try (BufferedReader in = new BufferedReader(new InputStreamReader(httpClient.getInputStream()))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
response.append(line);
}
// print result
System.out.println(response.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In both cases I get timed out.
Error in the first case:
org.apache.http.conn.HttpHostConnectException: Connect to www.google.com:443 [www.google.com/216.58.201.132] failed: Connection timed out: connect at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) at connCarPackage.INRIX.peticionGet(INRIX.java:53) at connCarPackage.INRIX.main(INRIX.java:26) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:368) at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142) ... 11 more
For the second case:
java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at sun.security.ssl.SSLSocketImpl.connect(Unknown Source) at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source) at sun.net.NetworkClient.doConnect(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.protocol.https.HttpsClient.(Unknown Source) at sun.net.www.protocol.https.HttpsClient.New(Unknown Source) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at java.net.HttpURLConnection.getResponseCode(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source) at urlConnection.INRIXURL.peticionGet(INRIXURL.java:31) at urlConnection.INRIXURL.main(INRIXURL.java:14)
I think I might be related to the proxy configuration at my workplace, but I don't know how to check or fix it if that's the case or if I'm just not doing the request properly. I noticed that I can't ping sites or machines outside the company network but I don't know if it is related.
Also using https://reqbin.com/ the request works fine and I can browse without problems.