Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP requests with basic authentication

Tags:

I have to download and parse XML files from http server with HTTP Basic authentication. Now I'm doing it this way:

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(); 

But in that way I can't get xml (or I'm just simply not aware of that ) document from server with http authentication.

I will be really grateful if you can show me the best and easiest way to reach my goal.

like image 545
sanczez Avatar asked Jan 07 '11 15:01

sanczez


People also ask

Can HTTP can control basic authentication?

HTTP basic authentication is a simple challenge and response mechanism with which a server can request authentication information (a user ID and password) from a client. The client passes the authentication information to the server in an Authorization header.

How does HTTP basic authentication work?

Basic authentication sends user names and passwords over the Internet as text that is Base64 encoded, and the target server is not authenticated. This form of authentication can expose user names and passwords. If someone can intercept the transmission, the user name and password information can easily be decoded.

How do you use Basic Auth with requests in python?

To achieve this authentication, typically one provides authentication data through Authorization header or a custom header defined by server. Replace “user” and “pass” with your username and password. It will authenticate the request and return a response 200 or else it will return error 403.


1 Answers

You can use an Authenticator. For example:

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

This sets the default Authenticator and will be used in all requests. Obviously the setup is more involved when you don't need credentials for all requests or a number of different credentials, maybe on different threads.

Alternatively you can use a DefaultHttpClient where a GET request with basic HTTP authentication would look similar to:

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() 

I recommend using the latter because it gives you a lot more control (e.g. method, headers, timeouts, etc.) over your request.

like image 153
Josef Pfleger Avatar answered Oct 29 '22 16:10

Josef Pfleger