Google日历 - 获取ics文件(不含API)

时间:2013-10-29 13:04:49

标签: java curl google-calendar-api icalendar

我想在Java项目中从Google Agenda获取ICS文件,而不使用API​​(仅获取ics的内容)。 日历设置为“公开”,我从参数(https://www.google.com/calendar/ical/perrine.lbc62%40gmail.com/public/basic.ics)获取公共地址。 如果我在浏览器中尝试此地址,我可以下载该文件(如果我没有连接,则为事件)。

但是,如果我尝试使用curl命令行或使用我的Java代码获取内容,则它不起作用:我无法连接到URL。

Curl说:curl:(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)

你知道为什么我在这种情况下无法访问网址,我该怎么办才能访问它?

非常感谢您的回答:)

1 个答案:

答案 0 :(得分:0)

您是否在企业环境中?这听起来像代理设置问题,特别是考虑到您从浏览器访问它。

使用--proxy选项尝试curl(参见例如Linux curl command with proxy