• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Converting to HttpClient

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi -

I have the following code (that works by the way) but our vendor is suggesting that we use HttpClient when calling out to them. For the life of me, I cannot rewrite this little itty bitty piece of code using HttpClient (either commons v 3.1 or the new httpclient beta 4) and get it to work!!!

URL url = new URL("http://tsparcs02:9080/apex/api/query? filtername=USER_FLTR&PARM_USER_ID=maher&usn=n4api1&pwd=n4api&mode=SNX&operatorId=MAH&complexId=USNWK&facilityId=PORTELIZABETH&yard=MAIN.xml");

HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

String userPassword = username + ":" + password;
// Base64 encode the authentication string
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.
getBytes());
// Set Basic Authentication parameters
con.setRequestProperty("Authorization", "Basic " + encoding);
con.connect();
DataInputStream dis = new DataInputStream(con.getInputStream());

If anyone can help me out I would appreciate it.

Jennifer
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless I'm missing something, I'd say that looks pretty much like what this example shows.

Oops, forgot to link the example: http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/BasicAuthenticationExample.java?view=markup
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what's wrong with java.net.URL in this case, except for using the internal Sun base64 encoder class, but it should be easy to do the same with HttpClient:
 
Jennifer Moran
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi yes - I am sorry for not being clear.

I was able to code this piece in HttpClient using the examples I found, I just cannot get it to work. I keep getting "unauthorized 401". I think my problem may be that in the examples the url used is an http with a com at the end. I have a complex url that has parameters appended to it. So what do I use for the authentication piece and what do I use for the getmethod piece?

Also, I am assuming I do not have to encode the "username" "password" like I have to do with the java.net (because I didn't) ???

Thanks again for the help.


Jennifer
 
Jennifer Moran
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind I got it.....sometimes you just need to go home and rest and have at it again the next day!!

Appreciate the help!

Jennifer
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic