0

I've got an XML file on a server. It is protected with an standard Http-Authorization. I want to parse the file. So what is the right way to get the file to the parser?

The authentification string I already have (Base64).

The XML looks like:

    <action>
      <class>11/1,11/2,11/3,11/4/ lat1</class>
      <hour>1</hour>
      <lesson>Eng</lesson>
      <teacher>Schr</teacher>
      <room>E312</room>
      <info>for Lat Gri </info>
    </action>
    <action>
      <class>11/1,11/2,11/3,11/4/ lat1</class>
      <hour>2</hour>
      <lesson>Eng</lesson>
      <teacher>Schr</teacher>
      <room>E312</room>
      <info>for Lat Gri </info>
    </action>
Grevius
  • 51
  • 9
  • What's wrong with requesting the file with the `Authorization` header set? – marekful Jan 19 '14 at 12:38
  • I'm sorry, I am really new to this and don't know how to do that. I already looked up many examples but none worked for me... Edit: Do you mean [that](http://stackoverflow.com/questions/4627395/http-requests-with-basic-authentication)? – Grevius Jan 19 '14 at 12:40

1 Answers1

1

To request a resource over HTTP using Basic Authentication, you need to construct and add the Authorization header line to the request as described in the linked document.

Basically, you add a header line to the request. The name of the field is Authorization and the value is the auth type followed by a space and then the base64 encoded string of the username:password.

So for e.g. if the username is John and the password is secret, you would form a string:

John:secret

and then base64 encode it which will result in:

Sm9objpzZWNyZXQ=

So the full header line to add to the request looks like this:

Authorization: Basic Sm9objpzZWNyZXQ=

Hope this helps.

marekful
  • 14,986
  • 6
  • 37
  • 59
  • So you meane something like `HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://mysitewithbasicauth.com/test.xml"); httpGet.addHeader("Authorization", "Basic c2NodWVsZXI6aGapbmU3NA==");` and then I end up with a HttpEntity. – Grevius Jan 21 '14 at 16:37
  • I don't know this HttpClient you are working with, but given the header is added _before_ the request is sent, it should be fine. – marekful Jan 21 '14 at 16:39
  • Bascially I don't have anything yet. I need to know how to do it, because I'm only a beginner... sorry – Grevius Jan 21 '14 at 17:02
  • Just try. The example in the above comment looks OK. – marekful Jan 21 '14 at 17:03