我想用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的配置问题,因为它适用于第一个网址...
Morevoer,我可以使用我的浏览器从第二个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: 它看起来像是因为我的环境:我尝试了远程服务器,它似乎工作!但是为什么它对一些网址是好的,而对其他网站则是错误的...... 没有其他想法吗?