vaibhavbarmy
vaibhavbarmy

Reputation: 339

Url open with username and password

I have started to learn scala , the only other language I know is python. I am trying to write a code in scala which I have written in python. In that code I have to open a url thats in xml format which require a username and password and then parse it and get the elements which matches string name=ID

Here is my python code

import urllib2

theurl = 'Some url'
username = 'some username'
password = 'some password'
# a great password

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
# this creates a password manager
passman.add_password(None, theurl, username, password)
# because we have put None at the start it will always
# use this username/password combination for  urls
# for which `theurl` is a super-url

authhandler = urllib2.HTTPBasicAuthHandler(passman)
# create the AuthHandler

opener = urllib2.build_opener(authhandler)

urllib2.install_opener(opener)
# All calls to urllib2.urlopen will now use our handler
# Make sure not to include the protocol in with the URL, or
# HTTPPasswordMgrWithDefaultRealm will be very confused.
# You must (of course) use it when fetching the page though.

pagehandle = urllib2.urlopen(theurl)
# authentication is now handled automatically for us


##########################################################################

from xml.dom.minidom import parseString

file = pagehandle

#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()
#parse the xml you downloaded
dom = parseString(data)
#retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName:
xmlData=[]
for s in dom.getElementsByTagName('string'):
    if s.getAttribute('name') == 'ID':
        xmlData.append(s.childNodes[0].data)
print xmlData

and this is what I have written in scala to open a url I am still to figure out how to handle it with username password I have searched on internet regarding the same and still didnt get what I am looking for.

object URLopen {
  import java.net.URL
  import scala.io.Source.fromURL
  def main(arg: Array[String]){
    val u = new java.net.URL("some url")
    val in = scala.io.Source.fromURL(u) 
    for (line <- in.getLines)
      println(line)
    in.close()
  }
}

Can someone help me to handle the url open with username password or tell me from where I can learn how to do it? In python we have docs.python.org/library/urllib2.html where I can learn about various modules and how to use them. Is there some link for scala too?

PS: Any help regarding parsing xml and getting the elements with string name= ID is also welcomed

Upvotes: 2

Views: 3416

Answers (1)

flavian
flavian

Reputation: 28511

What you are describing is Basic Authentication in which case you need:

import org.apache.commons.codec.binary.Base64;

object HttpBasicAuth {
   val BASIC = "Basic";
   val AUTHORIZATION = "Authorization";

   def encodeCredentials(username: String, password: String): String = {
      new String(Base64.encodeBase64String((username + ":" + password).getBytes));
   };

   def getHeader(username: String, password: String): String = 
      BASIC + " " + encodeCredentials(username, password);
};

Then just add this as a request header.

import scala.io.Source
import java.net.URL

object Main extends App {
    override def main(args: Array[String]) {
      val connection = new URL(yoururl).openConnection
      connection.setRequestProperty(HttpBasicAuth.AUTHORIZATION, HttpBasicAuth.getHeader(username, password);
      val response = Source.fromInputStream(connection.getInputStream);
    }
}

Upvotes: 4

Related Questions