是否可以使用来自 restservice java 的 rss 提要?
Is it possible to consume rss feed from restservice java?
我正在尝试通过简单的休息服务调用来使用 rss 提要。但是我正在连接超时。
示例 rss 提要:https://www.yahoo.com/news/rss/
我的代码:
public class 测试 {
// 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:连接超时:connect
在 java.net.DualStackPlainSocketImpl.connect0(本机方法)
在 java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
在 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)
在 sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625)
在 sun.security.ssl.BaseSSLSocketImpl.connect(基础SSLSocketImpl.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)
在 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
在 sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933)
在 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301)
在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
在 Test.main
这是错误的吗approach/code?如果您能提出更好的建议 approach/code 那就太好了。
终于可以通过设置代理来解决问题了。这是对我有用的代码。
public class 测试 {
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();
}
}
我正在尝试通过简单的休息服务调用来使用 rss 提要。但是我正在连接超时。 示例 rss 提要:https://www.yahoo.com/news/rss/
我的代码:
public class 测试 {
// 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:连接超时:connect 在 java.net.DualStackPlainSocketImpl.connect0(本机方法) 在 java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) 在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) 在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 在 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) 在 sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625) 在 sun.security.ssl.BaseSSLSocketImpl.connect(基础SSLSocketImpl.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) 在 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) 在 sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933) 在 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) 在 Test.main
这是错误的吗approach/code?如果您能提出更好的建议 approach/code 那就太好了。
终于可以通过设置代理来解决问题了。这是对我有用的代码。
public class 测试 {
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();
}
}