java.io.IOException HttpURLConnection.getInputStream

时间:2013-05-30 12:50:12

标签: java url xml-parsing

简要介绍:

我有一个url保存xml文件的内容。 我想解析这个文件,我使用下面的代码:

private String GetFileUrl() {
    return "https://dl-web.dropbox.com/get/My%20Projects/Xml%20Files/Vk%20Iu%20Quilling/KeyList.xml?w=AAD6Cf_YdXRg5tyY4cquyiXBZ8XuQUsIbsMGVoIfkgPcpg";
}

private NodeList SetUpXmlParserUrl() {
    try {
        URL xmlFile = new URL(fileUrl);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        InputStream inputStream = xmlFile.openStream();
        Document doc = dBuilder.parse(inputStream);

        //Get Node need to be parse.
        NodeList productNodeList = doc.getElementsByTagName("Item");
        return productNodeList;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

当此代码运行时,我收到此错误:

java.io.IOException: Server returned HTTP response code: 403 for URL: https://dl-web.dropbox.com/get/My%20Projects/Xml%20Files/Vk%20Iu%20Quilling/KeyList.xml?w=AAD6Cf_YdXRg5tyY4cquyiXBZ8XuQUsIbsMGVoIfkgPcpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

任何人都可以帮忙..

3 个答案:

答案 0 :(得分:0)

我的猜测是你需要插入文件,你在浏览器上这样做但你的应用程序没有授权这样做。

HTTP 403表示URL是禁止的。

答案 1 :(得分:0)

通常你应该使用dropbox api,就像这个例子:

public class DropboxTest {

private static final String APP_KEY = "APP KEY";
private static final String APP_SECRET = "SECRET KEY";
private static final AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
private static DropboxAPI<WebAuthSession> mDBApi;

public static void main(String[] args) throws Exception {
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    WebAuthSession session = new WebAuthSession(appKeys, ACCESS_TYPE);
    WebAuthInfo authInfo = session.getAuthInfo();

    RequestTokenPair pair = authInfo.requestTokenPair;
    String url = authInfo.url;

    Desktop.getDesktop().browse(new URL(url).toURI());
    JOptionPane.showMessageDialog(null, "Press ok to continue once you have authenticated.");
    session.retrieveWebAccessToken(pair);

    AccessTokenPair tokens = session.getAccessTokenPair();
    System.out.println("Use this token pair in future so you don't have to re-authenticate each time:");
    System.out.println("Key token: " + tokens.key);
    System.out.println("Secret token: " + tokens.secret);

    mDBApi = new DropboxAPI<WebAuthSession>(session);
    System.out.println();
    System.out.print("Uploading file...");
    String fileContents = "Hello World!";
    ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContents.getBytes());
    Entry newEntry = mDBApi.putFile("/testing.txt", inputStream, fileContents.length(), null, null);
    System.out.println("Done. \nRevision of file: " + newEntry.rev);

}

}

答案 2 :(得分:0)

您访问的url不允许使用java访问。检查您的连接并将代理主机/端口设置为您的java连接。

或者您可以将其视为HttpRequest的响应,也可以这样做