具有基本authentication的HTTP请求

我必须使用 HTTP基本身份validation从http服务器下载和parsingXML文件。 现在我正在这样做:

URL url = new URL("http://SERVER.WITHOUT.AUTHENTICATION/some.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(url.openStream())); doc.getDocumentElement().normalize(); 

但在这种方式,我不能得到XML(或者我只是根本没有意识到)从HTTP身份validation服务器的文件。

如果您能向我展示实现目标的最好和最简单的方法,我将非常感激。

Solutions Collecting From Web of "具有基本authentication的HTTP请求"

你可以使用一个Authenticator 。 例如:

 Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "user", "password".toCharArray()); } }); 

这将设置默认的Authenticator ,并将在所有请求中使用。 显然,当你不需要所有请求的证书或许多不同的证书(可能在不同的线程上)时,设置更多地涉及到。

或者,您可以使用DefaultHttpClient ,其中具有基本HTTP身份validation的GET请求看起来类似于:

 HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://foo.com/bar"); httpGet.addHeader(BasicScheme.authenticate( new UsernamePasswordCredentials("user", "password"), "UTF-8", false)); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity responseEntity = httpResponse.getEntity(); // read the stream returned by responseEntity.getContent() 

我build议使用后者,因为它可以提供更多的控制(例如方法,标题,超时等)。

 public String reloadTomcatWebApplication(String user, String pwd, String urlWithParameters, boolean returnResponse) { URL url = null; try { url = new URL(urlWithParameters); } catch (MalformedURLException e) { System.out.println("MalformedUrlException: " + e.getMessage()); e.printStackTrace(); return "-1"; } URLConnection uc = null; try { uc = url.openConnection(); } catch (IOException e) { System.out.println("IOException: " + e.getMessage()); e.printStackTrace(); return "-12"; } String userpass = user + ":" + pwd; String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); uc.setRequestProperty("Authorization", basicAuth); InputStream is = null; try { is = uc.getInputStream(); } catch (IOException e) { System.out.println("IOException: " + e.getMessage()); e.printStackTrace(); return "-13"; } if (returnResponse) { BufferedReader buffReader = new BufferedReader(new InputStreamReader(is)); StringBuffer response = new StringBuffer(); String line = null; try { line = buffReader.readLine(); } catch (IOException e) { e.printStackTrace(); return "-1"; } while (line != null) { response.append(line); response.append('\n'); try { line = buffReader.readLine(); } catch (IOException e) { System.out.println(" IOException: " + e.getMessage()); e.printStackTrace(); return "-14"; } } try { buffReader.close(); } catch (IOException e) { e.printStackTrace(); return "-15"; } System.out.println("Response: " + response.toString()); return response.toString(); } return "0"; } 

使用HttpClient。 用HTTP AUTH执行下载的文档就在这里 。 获取string结果的文档在这里 。 然后,parsing你的string(最好使用SAX,但不是DOM)。

  • 不推荐使用DefaultHttpClient
  • addHeader必须有2个参数

使用HttpClient 4.5.2更新代码块

 HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet("https://test.com/abc.xyz"); httpGet.addHeader("Authorization", BasicScheme.authenticate(new UsernamePasswordCredentials("login", "password"), "UTF-8")); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity responseEntity = httpResponse.getEntity();