0

简要说明:

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

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)

任何人都可以帮忙请..

4

3 回答 3

0

我的猜测是您需要登录才能访问该文件,您是在浏览器上进行的,但您的应用程序没有这样做的授权。

HTTP 403 表示 URL 被禁止。

于 2013-05-30T12:53:16.927 回答
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);

}

}

于 2013-05-30T12:54:30.833 回答
0

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

或者您可以将其读取为 HttpRequest 的响应,这也是可行的

于 2013-05-30T12:54:33.433 回答