1

我想用 Java 应用程序下载 ics 文件,所以我只是尝试了这个页面上的代码:http: //hc.apache.org/httpclient-legacy/tutorial.html(在底部)。

很多网址都很好,但是这个给了我标题错误:http ://www.sportycal.com/cal/getIcs/id/264/remider/1/ct/any/sportycal.ics

我认为这不是我的 IDE 的配置问题,因为它适用于第一个 url...

此外,我可以使用浏览器从第二个 URL 下载文件,并得到以下标题:

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

请求标头

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

响应标头

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

我想到了这个:连接:关闭。我寻求了 RFC,但我真的不明白它的含义。你认为非持久连接可能是它不起作用的原因吗?我该怎么做才能得到这个文件?

预先感谢您的帮助!

(对不起,如果我的英语不是很好,我希望你能理解......)

编辑 :

我尝试的代码与教程完全相同:

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();
    }  


}

我得到的堆栈跟踪如下:

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)

编辑2:看起来是因为我的环境:我在远程服务器上试过,它似乎工作!但是为什么某些网址可以,而其他网址则错误……请没有其他想法?

4

0 回答 0