1

我正在尝试实现 JSoup API 来解析 XML,但我在使用Authentication Required Dialog进行身份验证时遇到问题。有没有办法在 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));

提前致谢!

4

1 回答 1

1

我无法使用 JSoup 进行身份验证,但我设法解决了问题。我所做的是实现org.apache.commons.httpclient.HttpClientorg.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 
于 2013-09-13T07:43:08.097 回答