我想在 Java 项目中从 Google Agenda 获取 ICS 文件,而不使用 API(只是获取 ics 的内容)。日历设置为“公共”,我从参数( https://www.google.com/calendar/ical/perrine.lbc62%40gmail.com/public/basic.ics)获得公共地址。如果我在浏览器中尝试这个地址,我可以下载文件(如果我没有连接的话)。
但是,如果我尝试使用 curl 命令行或我的 Java 代码获取内容,它就不起作用:我无法连接到 URL。
卷曲说:卷曲:(7)无法连接到主机
Java代码如下:
private ICalendar getICalWithUrl(String url) throws Exception {
ICalendar iCal = null;
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
throw new Exception("Unable to get content at the url "+url+". Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
iCal=BiweeklyManager.parse(new String(responseBody));
} finally {
// Release the connection.
method.releaseConnection();
}
return iCal;
}
堆栈跟踪如下:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.<init>(SSLSocketImpl.java:394)
at com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:123)
at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:81)
at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:126)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:706)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:386)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
您知道为什么在这种情况下我无法访问该 url,我该怎么做才能访问它?
非常感谢你的回答 :)