2013-10-29 68 views
0

我想在Java項目中從Google議程獲取ICS文件,而不使用API​​(只是獲取ics的內容)。 日曆設置爲「公開」,我從參數中獲得公共地址(https://www.google.com/calendar/ical/perrine.lbc62%40gmail.com/public/basic.ics)。 如果我在瀏覽器中嘗試此地址,我可以下載該文件(如果我沒有連接,則爲事件)。Google日曆 - 獲取ics文件(不含API)

但是,如果我嘗試使用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) 

待辦事項你知道爲什麼我在這種情況下無法訪問網址,我能做些什麼來訪問它?

非常感謝您的回答:)

+0

,準確的URL在捲曲對我的作品... –

回答

0

您是否在企業環境?這聽起來像是一個代理設置問題,特別是考慮到你從瀏覽器訪問它。

與--proxy選項嘗試捲曲(見例如Linux curl command with proxy

相關問題