我正在尝试通过简单的休息服务调用来消费rss feed。但我得到连接超时。 示例rss feed:https://www.yahoo.com/news/rss/
我的代码:
公共课测试{
// http://localhost:8080/RESTfulExample/json/product/get
public static void main(String a[]){
try {
URL url = new URL("https://www.yahoo.com/news/rss/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
//conn.setConnectTimeout(999999);
/*
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}*/
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
例外:
java.net.ConnectException:连接超时:连接 at java.net.DualStackPlainSocketImpl.connect0(Native Method) 在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 在java.net.Socket.connect(Socket.java:579) at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625) at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160) 在sun.net.NetworkClient.doConnect(NetworkClient.java:180) 在sun.net.www.http.HttpClient.openServer(HttpClient.java:432) 在sun.net.www.http.HttpClient.openServer(HttpClient.java:527) 在sun.net.www.protocol.https.HttpsClient。(HttpsClient.java:275) 在sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) 在sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) 在Test.main
这是错误的方法/代码吗?如果你能提出更好的方法/代码,那将是非常好的。
答案 0 :(得分:0)
最后能够通过设置代理来解决问题。这是适合我的代码。
公共课测试{
public static void main(String a[]) throws ClientProtocolException, IOException{
HttpClient httpClient = new DefaultHttpClient();
HttpHost proxy=new HttpHost("hostname", portnumber, "scheme"); // ex: hostname="your org proxy host" portnumber=8080 scheme="http"
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,proxy );
HttpGet getRequest = new HttpGet(
"https://www.yahoo.com/news/rss/");
//getRequest.addHeader("accept", "application/xml");
final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
getRequest.setConfig(config);
HttpResponse response = httpClient.execute(getRequest);
/*if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}*/
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
httpClient.getConnectionManager().shutdown();
}
}