|
|
本帖最后由 Test 于 2012-3-16 14:51 编辑
Chapter 5: Using SSL in Java Clients Creating HTTP and HTTPS connections in Java applications
You can create HTTP connections in Java applications usingthe HTTP protocol handling code built in to the Java Developer’sKit, and HTTPS connections using the HTTPS protocol handler providedwith EAServer.
HTTP connections
The standard Java virtual machine provides HTTP connectivitywith these classes in java.net package:
- URL allows you to use UniformResource Locator strings for HTTP connections and other protocolconnections that can be represented by URLs.
- URLConnection represents a connectionto a server and resource indicated by a URL.
- HttpURLConnection extends URLwith additional methods that are specific to the HTTP protocol.
For details on these classes, see the JDK documentation. Thefollowing code shows a typical example. This code opens a connection,retrieves the data (text is assumed), and prints it:
- URL url = new URL("http://www.sybase.com/");
- URLConnection conn = url.openConnection();
- conn.connect();
- InputStreamReader content
- = new InputStreamReader(conn.getInputStream());
- for (int i=0; i != -1; i = content.read())
- {
- System.out.print((char) i);
- }
复制代码
HTTPS connections
The procedure for creating HTTPS connections is similar tothat for HTTP connections, except that you must install EAServer’sHTTPS protocol handler in the Java virtual machine and configureSSL parameters before opening a connection.
System requirements
EAServer’s HTTPS protocol handler uses the same SSLimplementation as used by Java and C++ IIOP clientsand requires a full client runtime install. For information on systemrequirements, see “Requirements”.
Installing the HTTPS protocol handler
The EAServer HTTPS protocol handler can be installed two ways:
- By configuring the java.protocol.handler.pkgs Javasystem property, making it the default handler for all HTTPS URLs.This is the recommended approach if you do not need to use anothervendor’s HTTPS protocol handler in addition to the EAServerimplementation.
- By calling one of the java.net.URL constructorsthat takes a java.net.URLStreamHandler as a parameter.This approach must be used if you must use more than one HTTPS protocolhandler in one EAServer or in one client application.
Configuring the default protocol handlers
The java.protocol.handler.pkgs Javasystem property configures the Java virtual machine default URLprotocol handlers. To use the EAServer handlers, you must add com.sybase.jaguar.net tothe list. For more information on this property, see the documentationfor java.net.URL in JDK 1.2.
In a client application, specify this property on the commandline; for example:
jre -Djava.protocol.handler.pkgs=com.sybase.jaguar.net ...
For an EAServer, set the JVM options property using the Advancedtab in the Server Properties dialog box:
Property
| Value
| com.sybase.jaguar.server.jvm.options
| If not already set, set to:
-Djava.protocol.handler.pkgs=com.sybase.jaguar.net
If already set, verify that the value includes this option.JVM options must be separated with a comma.
|
You can specify more than one package by separating packagenames with a | (pipe) character, but you can configureonly one handler per protocol.
Specifying protocol handlers at runtime
If you must use more than one HTTPS protocol handler in oneEAServer or in one client application, you must call one of the java.net.URL constructorsthat takes a java.net.URLStreamHandler as a parameter.The specified java.net.URLStreamHandler instanceoverrides the default handler for the protocol specified by theURL. For example, to specify the EAServer HTTPS handler, use codelike this:
- import java.net.*;
- import com.sybase.jaguar.net.JagURLStreamHandlerFactory;
- import com.sybase.jaguar.net.HttpsURLConnection;
- ....
- String url_string = "https://localhost:8081/index.html";
- // The URL stream handler factory is required to create a stream
- // handler.
- JagURLStreamHandlerFactory fact = new JagURLStreamHandlerFactory();
- // Extract the protocol from the front of the URL string
- String protocol = url_string.substring(0, url_string.indexOf(":"));
- // If the protocol is HTTPS, use the EAServer HTTPS handler. Otherwise,
- // use the default handler
- java.net.URL url;
- if (protocol.equals("https"))
- {
- url = new URL((URL)null, url_string,
- fact.createURLStreamHandler(protocol));
- } else
- {
- url = new URL(url_string);
- }
复制代码
EAServer’s HttpsURLConnection class
EAServer provides the com.sybase.jaguar.net.HttpsURLConnection classto support HTTPS connectivity. This class extends java.net.URLConnection and implementsall methods of java.net.HttpURLConnection. HttpsURLConnection providesthese additional methods specifically for SSL support:
- A setSSLProperty method with signature:
- void setSSLProperty (String prop, String value) throws
- CtsSecurity.InvalidPropertyException,
- CtsSecurity.InvalidValueException
复制代码
Call this method to set the SSL properties described in “SSL properties”. - A setSSLProperties method withsignature:
- void setSSLProperty (java.util.Properties props) throws
- CtsSecurity.InvalidPropertyException,
- CtsSecurity.InvalidValueException
复制代码
This method is the same as setSSLProperty,but allows you to set multiple properties with one call. - A getSSLProperty method withsignature:
- void setGlobalProperty (String prop, String value) throws
- CtsSecurity.InvalidPropertyException,
- CtsSecurity.InvalidValueException
复制代码
Call this method to retrieve the SSL properties describedin “SSL properties”. - A setGlobalProperty method withsignature:
- String[] getGlobalProperty(String prop) throws
- CtsSecurity.InvalidPropertyException;
复制代码
Call this method to set the global SSL properties describedin “SSL properties”.Properties set with this method affect the handling of all HTTPSconnections, not just the current one. - A getGlobalProperty method withsignature:
- CtsSecurity.SSLSessionInfo getSessionInfo() throws CtsSecurity.SSLException
复制代码
Call this method to retrieve the global SSL properties describedin “SSL properties”. - A getSessionInfo method withsignature:
CtsSecurity.SSLSessionInfo getSessionInfo() throws CtsSecurity.SSLExceptionThe SSLSessionInfo methods allow you todetermine the SSL session properties, such as the server’saddress, the client certificate in use, the server certificate inuse, and so forth. For more information, see the Interface Repositorydocumentation for the CtsSecurity::SSLSessionInfo IDLinterface. getSessionInfo throws an a SSLException instanceif SSL is not used on the connection.
Creating HTTPS connections
- Configure or install the EAServer HTTPS protocolhandler as described in “Installing the HTTPS protocol handler”.
- Create URL and URLConnection instances.If connecting to an EAServer, specify the address of an HTTPS listenerthat
- URL url = new URL("https://myhost:8081/index.html");
- URLConnection conn = url.openConnection();
复制代码
- Verify that the object returned by URL.openConnection isof class com.sybase.jaguar.net.HttpsURLConnection,then set SSL properties for the connection. “SSL properties” describesthe SSL properties that can be set. At a minimum, you must specifythe qop and pin properties, as well as the certificateLabel propertyif using mutual authentication. For example:
- if (conn instanceof HttpsURLConnection)
- {
- HttpsURLConnection https_conn = (HttpsURLConnection) conn;
- try
- {
- https_conn.setSSLProperty( "qop","sybpks_intl" );
- https_conn.setSSLProperty( "pin", "secret");
- https_conn.setSSLProperty(
- "certificateLabel", "John Smith");
- }
- catch ( CtsSecurity.InvalidPropertyException ipe )
- {
- System.err.println( ipe );
- }
- catch ( CtsSecurity.InvalidValueException ive )
- {
- System.err.println( ive );
- }
复制代码
- Open the connection, for example:
|
|