我正在使用某些应用程序的API下载文档。 问题是,当我尝试解析inputStream时,它将引发响应代码409异常。 但是最大的问题是它有时会抛出此异常。 此异常的原因是什么?
这是我的代码;
URL url = new URL(urlStr);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
if (conn != null) {
try {
InputStream inputStream = conn.getInputStream();//this line throws exception
MyXmlParser myXmlParser = new MyXmlParser();
MyClientData myClientData = myXmlParser.parseMyOutput(inputStream);
return myClientData;
} catch (IOException e) {
throw e;
}
}
这是我的XML解析器;
public class MyXmlParser {
private final JAXBContext myOutputContext;
private final XMLInputFactory xif;
public MyXmlParser() throws JAXBException {
xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
myOutputContext = JAXBContext.newInstance(MyClientData.class);
}
public MyClientData parseMyOutput(InputStream inputStream) throws JAXBException {
Unmarshaller jaxbUnmarshaller = myOutputContext.createUnmarshaller();
return (MyClientData) jaxbUnmarshaller.unmarshal(createStreamReader(inputStream));
}
private XMLStreamReader createStreamReader(InputStream inputStream) throws JAXBException {
try {
return xif.createXMLStreamReader(inputStream);
} catch (XMLStreamException ex) {
throw new JAXBException(ex);
}
}
}
这是从应用程序的API中获取的,用于下载文档;
HTTP Request GET /{file_id}/download
我遇到错误了;
java.io.IOException: Server returned HTTP response code: 409 for URL: https://5.5.5.5:443/4144444/download