Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLConnection Doubts -- Urgent!!

 
Anshumn Sagar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have two doubts :
1. I am trying to connect to some site using URLConnection. But I am behind a proxy. How can I do it to connect to that site? In my code I use the following to cross proxy :
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "my proxy ip" );
System.getProperties().put( "proxyPort", "8080" );
String password = "username assword";
String encodedPassword = "Base " + new sun.misc.BASE64Encoder().encode( password.getBytes() );
URL url = new URL("http://www.rediff.com");
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty( "Proxy-Authorization", encodedPassword );
urlConnection.setDoOutput(true);
But it does not cross the proxy. Am I doing something wrong in the above piece of code ? Any other way to do it ??
2. Is it possible in JDK 1.1 to make a HTTPS connection ? In the above code I tried, but I get error saying that the protocol is not recognised. Is there any way to do it using jdk 1.1 ?
If not then, any new version of jdk supports HTTPS connection ?
Please give your valuable inputs on the same.
Any help in this shall be highly very helpful for me.
Thanks in advance,
Anshumn
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Correct me if I'm wrong. It might be as simple as:
String password = "username:password";

Or if that does not work, try to encode your string to bytes using a character set. Default set might not be what you want:
String encodedPassword = new sun.misc.BASE64Encoder().encode( password.getBytes("US-ASCII") );

Java handles HTTPS through the package JSSE which you'll find here:
http://java.sun.com/products/jsse/

Yours
- Lars
 
Anshumn Sagar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lars,
I was trying the same as "username assword";. It is not working. Now I specified the charater set as ("US-ASCII") also, but still it is not crossing the proxy. I am not able to figure out what can be the problem. Please let me know if any other alternative is there.
Thanks for the help..
Anshumn
Originally posted by Lars J. Nilsson:
Hello,
Correct me if I'm wrong. It might be as simple as:
String password = "username assword";

Or if that does not work, try to encode your string to bytes using a character set. Default set might not be what you want:
String encodedPassword = new sun.misc.BASE64Encoder().encode( password.getBytes("US-ASCII") );

Java handles HTTPS through the package JSSE which you'll find here:
http://java.sun.com/products/jsse/

Yours
- Lars

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic