我正在尝试发出一个简单的 GET 请求:
https://www.google.com/search?q=stuff
我使用了 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();
}
}
}
和 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();
}
}
}
在这两种情况下,我都会超时。
第一种情况的错误:
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
对于第二种情况:
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)
我想我可能与我工作场所的代理配置有关,但如果是这种情况,或者我没有正确执行请求,我不知道如何检查或修复它。我注意到我无法 ping 公司网络外的站点或机器,但我不知道它是否相关。
还使用 https://reqbin.com/请求工作正常,我可以毫无问题地浏览。
最佳答案
如你所说 :
I think I might be related to the proxy configuration at my workplace, [...]
你应该问你的 IT 如何使用您公司的代理(或检查您的网络设置,这可能会显示代理设置)
也看看这里:
https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html
关于java - 尝试发出简单的 Http GET 请求时连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58933418/