• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting to HttpClient  RSS feed

 
Jennifer Moran
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!