java.io.IOException HttpURLConnection.getInputStream
简介: 我有一个url保存xml文件的内容。 我想解析此文件以使用以下代码:java.io.IOException HttpURLConnection.getInputStream,java,url,xml-parsing,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 SetUpXmlPa
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)
有人能帮忙吗?我猜您需要登录才能访问该文件,您是在浏览器上登录的,但您的应用程序没有这样做的授权
HTTP 403表示URL被禁止。通常您应该使用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);
}
公共类DropboxTest{
私有静态最终字符串APP_KEY=“APP KEY”;
私有静态最终字符串APP_SECRET=“SECRET KEY”;
私有静态最终AccessType ACCESS\u TYPE=AccessType.APP\u文件夹;
私有静态DropboxAPI mDBApi;
公共静态void main(字符串[]args)引发异常{
AppKeyPair appKeys=新的AppKeyPair(应用程序密钥,应用程序密钥);
WebAuthSession会话=新的WebAuthSession(appKeys,访问类型);
WebAuthInfo authInfo=session.getAuthInfo();
RequestTokenPair=authInfo.RequestTokenPair;
字符串url=authInfo.url;
Desktop.getDesktop().browse(新URL.toURI());
showMessageDialog(null,“验证后按ok继续”);
会话。retrieveWebAccessToken(对);
AccessTokenPair tokens=session.getAccessTokenPair();
println(“以后使用此令牌对,这样您就不必每次都重新验证:”;
System.out.println(“密钥令牌:“+tokens.Key”);
System.out.println(“秘密令牌:“+tokens.Secret”);
mDBApi=新的DropboxAPI(会话);
System.out.println();
系统输出打印(“上传文件…”);
String fileContents=“你好,世界!”;
ByteArrayInputStream inputStream=新建ByteArrayInputStream(fileContents.getBytes());
Entry newEntry=mDBApi.putFile(“/testing.txt”,inputStream,fileContents.length(),null,null);
System.out.println(“完成。\n文件预览:“+newEntry.rev”);
}
}java访问不允许您提供的url。检查连接并将代理主机/端口设置为Java连接
或者你可以把它读作HttpRequest的响应,这也是可行的htp403:probled。您无权访问此URL。您需要通过dropbox进行身份验证。。检查他们是否提供了一种(编程)方法来实现这一点