SAXParser说:“java.net.SocketException:网络无法访问”,但xmlint或curl都可以

时间:2011-01-13 20:30:44

标签: java xml network-programming sax

我可以下载以下网址:“http://genome.ucsc.edu/cgi-bin/das/hg18/dna?segment=chr1%3A10%2C20”

xmllint

xmllint "http://genome.ucsc.edu/cgi-bin/das/hg18/dna?segment=chr1%3A10%2C20"
<?xml version="1.0" standalone="no"?>
<!DOCTYPE DASDNA SYSTEM "http://www.biodas.org/dtd/dasdna.dtd">
<DASDNA>
<SEQUENCE id="chr1" start="10" stop="20" version="1.00">
<DNA length="11">
ccctaacccta
</DNA>
</SEQUENCE>
</DASDNA>

卷曲

curl "http://genome.ucsc.edu/cgi-bin/das/hg18/dna?segment=chr1%3A10%2C20"
<?xml version="1.0" standalone="no"?>
<!DOCTYPE DASDNA SYSTEM "http://www.biodas.org/dtd/dasdna.dtd">
<DASDNA>
<SEQUENCE id="chr1" start="10" stop="20" version="1.00">
<DNA length="11">
ccctaacccta
</DNA>
</SEQUENCE>
</DASDNA>

但是以下java程序引发了异常:

import javax.xml.parsers.*;
public class Test
   {
   public static void main(String args[])
      {
      try
         {
         SAXParserFactory f=SAXParserFactory.newInstance();
     f.setNamespaceAware(false);
     f.setValidating(false);
         f.newSAXParser().parse(
            "http://genome.ucsc.edu/cgi-bin/das/hg18/dna?segment=chr1%3A10%2C20",
            new org.xml.sax.helpers.DefaultHandler()
            );
         System.out.println("OK");
         }
     catch(Exception err)
         {
         err.printStackTrace();
         }
      }
   }

错误:

java Test
java.net.ConnectException: Connection timed out
        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:525)
        at java.net.Socket.connect(Socket.java:475)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
        at sun.net.www.http.HttpClient.New(HttpClient.java:306)
        at sun.net.www.http.HttpClient.New(HttpClient.java:323)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
        at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
        at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
        at javax.xml.parsers.SAXParser.parse(SAXParser.java:277)
        at Test.main(Test.java:11)

为什么?

PS:这里没有代理。

1 个答案:

答案 0 :(得分:2)

(稍后) 好的,我有一个anwser:SAX解析器试图加载这个“http://www.biodas.org/dtd/dasdna.dtd”,这个站点已关闭。

但是如何阻止SAX解析器加载此URL ???