Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中从安全URL解组XML_Java_Url_Basic Authentication - Fatal编程技术网

在Java中从安全URL解组XML

在Java中从安全URL解组XML,java,url,basic-authentication,Java,Url,Basic Authentication,我试图访问一个安全url后面的XML文件。我想根据我已有的XSD绑定从url中解组该文件。这是我的方法 public void urlTest(String url){ URLConnection connection = null; JAXBContext jc; try { String userPassword = username + ":" + password; String encoding = new sun.misc.B

我试图访问一个安全url后面的XML文件。我想根据我已有的XSD绑定从url中解组该文件。这是我的方法

public void urlTest(String url){
    URLConnection connection = null;
    JAXBContext jc;

    try {
        String userPassword = username + ":" + password;
        String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());  
        URL add = new URL(this.url);            
        connection = add.openConnection();
        connection.setRequestProperty("Authorization", "Basic " + encoding);  
        connection.connect();

        jc = JAXBContext.newInstance("XML");
        Unmarshaller u = jc.createUnmarshaller();            
        Object o = u.unmarshal(connection.getURL());

        System.out.println("Complete");

    } catch (IOException ex) {
        Logger.getLogger(UrlDownload.class.getName()).log(Level.SEVERE, null, ex);
    } catch (JAXBException ex) {
        Logger.getLogger(UrlDownload.class.getName()).log(Level.SEVERE, null, ex);
    } 
}

我从中得到一个401服务器错误。我传递的用户名和密码是正确的。也许我遗漏了什么。

快速扫描后,看起来您是在向解编程序提供URL,而不是与授权的连接。

问题在于:

Object o = u.unmarshal(connection.getURL());
您正在从已经打开的连接中获取URl,并将其传递给解组器,解组器随后将打开它自己的连接,该连接将失败

您需要从刚刚打开的连接向解组器提供
InputStream
,例如

InputStream inputStream = connection.getInputStream();
Object o = u.unmarshal(inputStream);
然后在退出该方法之前:

inputStream.close();

我没有立即想到什么。我会用tcpdump检查发生了什么,如果没有加密,就用tcpdump检查,如果是,就用一些特技。我相信其他人可能会提出更复杂的建议;错误消息SCHWERWIEGEND:null javax.xml.bind.UnmarshaleException-带有链接异常:[org.xml.sax.SAXParseException:prolog中不允许内容。]有任何想法吗?Unmarshaller u=jc.createUnmarshaller()<代码>在此处输入代码InputStream InputStream=connection.getInputStream();对象o=u.unmarshal(输入流);但这会产生以下错误SCHWERWIEGEND:null javax.xml.bind.UnmarshalException-带有链接异常:[org.xml.sax.saxparsexception:prolog中不允许内容。]有什么想法吗?我将代码更改为以下内容。。。解组器u=jc.createUnmarshaller();InputStream InputStream=connection.getInputStream();但是我得到的错误是javax.xml.bind.UnmarshalException-带有链接异常:[org.xml.sax.SAXParseException:prolog中不允许内容。]Object o=u.unmarshal(inputStream);