1

I'm working on a web service and I need a code for making an HTTP request in java using basic authentication. I found lots of code at the internet but no one worked as expected. Does anyone knows some library and/or link to do this?

Here is a python equivalent to what I want...

import urllib2
import base64

req = urllib2.Request('http://testeserjaum.appspot.com/rest/metadata')
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
base64string = base64.encodestring('%s:%s' %('user', 'pass'))
print base64string
req.add_header("Authorization", "Basic %s" %base64string)
lol = urllib2.urlopen(req)
print lol.read()
user888360
  • 75
  • 1
  • 6

2 Answers2

3

Yea, you just use the HttpURLConnection class. The Android team has actually put a lot of development time into making this class extremely performant and usefully. I highly recommend it!

P.S. If you're really curious, checkout this blog post from the android dev team.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
0

You may want to check out Apache HTTP Client (http://hc.apache.org/httpcomponents-client-ga/). It makes HTTP communication pretty simple.

jeff
  • 4,325
  • 16
  • 27
  • The android dev team actually recommends you don't use this class for new apps. They don't do active development on it any more because it's so old and there's a bunch of backwards compatibility stuff that prevents them from making it run well. For details, checkout this blog post: http://android-developers.blogspot.com/2011/09/androids-http-clients.html – Kurtis Nusbaum Oct 26 '11 at 20:13