需要JSoup身份验证对话框

时间:2013-09-12 15:16:53

标签: java xml authentication jsoup

我正在尝试实现用于解析XML的JSoup API,但我在使用需要身份验证的对话进行身份验证时遇到问题。有没有办法在JSoup中实现它?

原始代码:

HttpClient http = new HttpClient();
http.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, new org.apache.commons.httpclient.NTCredentials(userName,password,url,domain));

提前致谢!

1 个答案:

答案 0 :(得分:0)

我无法使用JSoup进行身份验证,但我设法创建了一个解决方案。我所做的是实现 org.apache.commons.httpclient.HttpClient org.apache.commons.httpclient.methods.GetMethod 来连接和获取DataInputStreamReader。 DataInputStreamReader将传递给JSoup,因此我们可以使用JSoup来解析xml / html。请参阅以下代码:

// codes 

GetMethod method      = new GetMethod(url);

// passing the credentials
HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, new org.apache.commons.httpclient.NTCredentials(username,pass,url,domain));

Document doc = null; // org.jsoup.nodes.Document

if( http.executeMethod(method)==200){
    doc = Jsoup.parse(new DataInputStream(method.getResponseBodyAsStream()), "UTF-8", url);
}

// codes