1

I would like to download ics files with a Java application, so that I simply tried the code at this page : http://hc.apache.org/httpclient-legacy/tutorial.html (at the bottom).

All is right with many urls, but this one gaves me the error in title : http://www.sportycal.com/cal/getIcs/id/264/remider/1/ct/any/sportycal.ics

I think it's not a configuration problem from my IDE because it works for the first url...

Morevoer, I can download the file from the second URL with my browser, and I get this headers :

Request URL:url_du_site
Request Method:GET
Status Code:200 OK

Request Headers

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Host:www.sportycal.com
Proxy-Connection:keep-alive
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36

Response Headers

Access-Control-Allow-Origin:*
Connection:close
Content-Disposition:attachment; filename=cal.ics
Content-Type:text/calendar; charset=utf-8
Date:Fri, 25 Oct 2013 08:11:16 GMT
Server:Apache/2.2.14 (Ubuntu)
Set-Cookie:symfony=8p9gbbp301jhteu52e2qu1nie0; path=/
X-Cache:MISS from office-proxy.priv.atos.fr
X-Cache-Lookup:MISS from office-proxy.***.fr:3128
X-Powered-By:PHP/5.3.2-1ubuntu4.18

I thought about this one : Connection:close. I sought the RFC, but I don't really understand what it means.. Do you think the non-persistent connection could be the reason it does not work? What can I do to get this file?

Thank you by advance for your help!

(Sorry if my english is not really good, I hope you understand anyway...)

Edit :

The code I tried is really the same as the tutorial :

public static void main(String[] args) {
    String url = "http://www.sportycal.com/cal/getIcs/id/264/remider/1/ct/any/sportycal.ics";
    readUrl(url);
}


private static void readUrl(String url) {
    // 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) {
        System.err.println("Method failed: " + method.getStatusLine());
      }

      // Read the response body.
      byte[] responseBody = method.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not binary data
      System.out.println(new String(responseBody));

    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      method.releaseConnection();
    }  


}

And the stack trace I get is the following :

Fatal transport error: Connection timed out: connect
java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:249)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:79)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:121)
    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)
    at tests.TestUrl.readUrl(TestUrl.java:46)
    at tests.TestUrl.main(TestUrl.java:28)

Edit 2 : It looks like it is because of my environment : I tried on distant server and it seems to work !! But why it is okay for some urls, and wrong for others... No other ideas please ?

Perrine C
  • 21
  • 1
  • 5
  • Please show your downloading code, and a full stack trace of any exception it produces. – VGR Oct 25 '13 at 09:21
  • Non-persistent connection cannot prevent you from downloading a file, that's for sure. So Connection:close is not the cause here. I've tried example code from the tutorial and I'm able to download the file you specified. Please add the exact code you're trying to launch. – Jk1 Oct 25 '13 at 09:31
  • Your code is fine. The ConnectException seems to indicate a problem with either your Internet connection or some network between you and www.sportycal.com. In fact, the message of that exception was probably obtained from the native code which obtained it from the operating system. – VGR Oct 25 '13 at 10:28
  • But if I can get the file with my browser, this means it's not my Internet connection.. I used to think the response from the url was responsible, but if jk1 can get the page, the problem is somewhere else... I have no more ideas !!! :( – Perrine C Oct 25 '13 at 10:58
  • Try using standard Java classes to retrieve it: `try (InputStream stream = new URL("http://www.sportycal.com/cal/getIcs/id/264/remider/1/ct/any/sportycal.ics").openStream()) { Files.copy(stream, Paths.get("sportycal.ics"), StandardCopyOption.REPLACE_EXISTING); }` – VGR Oct 27 '13 at 13:22
  • The result seems to be exactly the same... Thanks for your help, if you have others ideas... :) – Perrine C Oct 28 '13 at 15:39

0 Answers0