Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do we HTTPS Post using Java and a proxy server? 1

Status
Not open for further replies.

rudejohn

IS-IT--Management
Jul 11, 2003
130
US
Our application needs to use a proxy server to do an HTTPS post to a third-party data vendor. The application is nearing launch date, and all the Googling in the world hasn't helped us yet... I was wondering if anyone here might be able to send me in the right direction. Here's what we're CURRENTLY doing:

Document myXML = null;
HttpURLConnection connection = null;
Properties property = System.getProperties();

final String USERNAME = "user";
final String USERPASSWORD = "pass";

String serverName = URL url = new URL(serverName);
connection = (HttpURLConnection) url.openConnection();

property.put("http.proxyHost", "my_proxy_host");
property.put("http.proxyPort", "666");

java.net.Authenticator.setDefault(new java.net.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new java.net.PasswordAuthentication(USERNAME,
USERPASSWORD.toCharArray());
}
});

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty(
"Content-Type",
"text/xml; charset=utf-8");

connection.setRequestMethod("POST");
connection.connect();

myXML = (Document) connection.getOutputStream();


It's not even getting past the seventh line URL = new URL(String). It throws an "invalid protocol: HTTPS" error when I do an e.getMessage(). Any help? TIA,

RJ


************
RudeJohn
************
 
A couple of pointers.

You need to install and use the Java Secure Sockets Extensions (JSSE) before you can use HTTPS.

Once done, you need to add the provider
Code:
/* need to use JSSE to change the stream handler to HTTPS. The URL class can't by default use HTTPS .  XmlRpc uses URL class to make it's connection. All JARs of JSSE included in class path also */

System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.[URL unfurl="true"]www.protocol");[/URL]

Class clsFactory = Class.forName("com.sun.net.ssl.internal.ssl.Provider");

if  ((clsFactory != null) && (Security.getProvider("SunJSSE") == null))
{
  Security.addProvider((Provider)clsFactory.newInstance());
}
You may also need to set the http system properties ...
Code:
System.setProperty("https.proxyHost", "myhost");
System.setProperty("https.proxyPort", "myport");

I've only been doing this for a few weeks myself, but hopefully this will help.

Greg.
 
Thank you... this definitely gets us on the right track. I don't know why we weren't able to find the information on JSSE, but... I'll post back here if we have any other questions. You're quite the savior!

RJ

************
RudeJohn
************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top