Jua*_*iel 4 java eclipse proxy http http-get
我正在尝试发出一个简单的 GET 请求:
https://www.google.com/search?q=stuff
Run Code Online (Sandbox Code Playgroud)
我使用过 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
和 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下我都会超时。
第一种情况的错误:
org.apache.http.conn.HttpHostConnectException:连接到 www.google.com:443 [www.google.com/216.58.201.132] 失败:连接超时:在 org.apache.http.impl.conn.DefaultHttpClientConnectionOperator 处连接。连接(DefaultHttpClientConnectionOperator.java:156)在org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374)在org.apache.http.impl.execchain.MainClientExec.建立Route(MainClientExec.java:393)在 org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) 在 org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) 在 org.apache.http.impl .execchain.RetryExec.execute(RetryExec.java:89) 在 org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) 在 org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient .java:185) 在 org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) 在 org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) 在 connCarPackage。 INRIX.peticionGet(INRIX.java:53) at connCarPackage.INRIX.main(INRIX.java:26) 引起:java.net.ConnectException:连接超时:在 java.net.DualStackPlainSocketImpl.connect0(本机方法)处连接java.net.DualStackPlainSocketImpl.socketConnect(未知源) 在 java.net.AbstractPlainSocketImpl.doConnect(未知源) 在 java.net.AbstractPlainSocketImpl.connectToAddress(未知源) 在 java.net.AbstractPlainSocketImpl.connect(未知源) 在 java.net.DualStackPlainSocketImpl.connectToAddress(未知源) net.PlainSocketImpl.connect(未知来源) 在 java.net.SocksSocketImpl.connect(未知来源) 在 java.net.Socket.connect(未知来源) 在 org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory. java:368) 在 org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142) ... 11 更多
对于第二种情况:
java.net.ConnectException:连接超时:在 java.net.DualStackPlainSocketImpl.connect0(本机方法)处连接,在 java.net.DualStackPlainSocketImpl.socketConnect(未知源)处,在 java.net.AbstractPlainSocketImpl.doConnect(未知源)处 连接。 net.AbstractPlainSocketImpl.connectToAddress(未知来源) 在 java.net.AbstractPlainSocketImpl.connect(未知来源) 在 java.net.PlainSocketImpl.connect(未知来源) 在 java.net.SocksSocketImpl.connect(未知来源) 在 java.net。 Socket.connect(未知来源)在sun.security.ssl.SSLSocketImpl.connect(未知来源)在sun.security.ssl.BaseSSLSocketImpl.connect(未知来源)在sun.net.NetworkClient.doConnect(未知来源)在sun。 net.www.http.HttpClient.openServer(未知来源) 在 sun.net.www.http.HttpClient.openServer(未知来源) 在 sun.net.www.protocol.https.HttpsClient.(未知来源) 在 sun.net .www.protocol.https.HttpsClient.New(未知来源)在sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(未知来源)在sun.net.www.protocol.http.HttpURLConnection.plainConnect0(未知来源)在 sun.net.www.protocol.http.HttpURLConnection.plainConnect(未知来源) 在 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(未知来源) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0 (未知来源)在sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源)在java.net.HttpURLConnection.getResponseCode(未知来源)在sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(未知来源)源)位于 urlConnection.INRIXURL.peticionGet(INRIXURL.java:31) 位于 urlConnection.INRIXURL.main(INRIXURL.java:14)
我认为我可能与工作场所的代理配置有关,但如果是这种情况或者我只是没有正确执行请求,我不知道如何检查或修复它。我注意到我无法 ping 通公司网络之外的站点或机器,但我不知道这是否相关。
另外使用https://reqbin.com/请求工作正常,我可以毫无问题地浏览。
小智 5
如你所说 :
我想我可能与我工作场所的代理配置有关,[...]
您应该询问您的IT如何使用您公司的代理(或检查您的网络设置,这可能会向您显示代理设置)
也可以看看这里:
https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html
| 归档时间: |
|
| 查看次数: |
10837 次 |
| 最近记录: |