Created
October 31, 2012 16:53
-
-
Save hachi-eiji/3988250 to your computer and use it in GitHub Desktop.
SSL Client Test(Digest authorize)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.InputStreamReader; | |
import java.security.KeyStore; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.HttpVersion; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.Credentials; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.conn.ClientConnectionManager; | |
import org.apache.http.conn.params.ConnManagerParams; | |
import org.apache.http.conn.scheme.Scheme; | |
import org.apache.http.conn.scheme.SchemeRegistry; | |
import org.apache.http.conn.ssl.SSLSocketFactory; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; | |
import org.apache.http.params.BasicHttpParams; | |
import org.apache.http.params.HttpParams; | |
import org.apache.http.params.HttpProtocolParams; | |
import org.junit.Test; | |
/** | |
* @author hachi-eiji | |
* | |
*/ | |
public class SSLClientTest { | |
@Test | |
public void post() throws Exception { | |
String host = ""; // send to data | |
int port = 443; // default port | |
String path = ""; // url path | |
String userName = ""; // for digest authorize | |
String pass = ""; // for digest authorize | |
String body = ""; // post data | |
String keystoreFile = ""; // key file path | |
String storePass = "changeit"; // key file pass | |
HttpParams params = new BasicHttpParams(); | |
ConnManagerParams.setMaxTotalConnections(params, 5); | |
ConnManagerParams.setTimeout(params, 1000); | |
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); | |
SchemeRegistry schemeRegistry = new SchemeRegistry(); | |
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); | |
FileInputStream instream = new FileInputStream(new File(keystoreFile)); | |
keystore.load(instream, storePass.toCharArray()); | |
instream.close(); | |
SSLSocketFactory sslSocketFactory = new SSLSocketFactory(keystore); | |
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); | |
schemeRegistry.register(new Scheme("https", sslSocketFactory, port)); | |
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); | |
Credentials credentials = new UsernamePasswordCredentials(userName, pass); | |
AuthScope scope = new AuthScope(host, port); | |
DefaultHttpClient client = new DefaultHttpClient(cm, params); | |
client.getCredentialsProvider().setCredentials(scope, credentials); | |
HttpPost post = new HttpPost("https://"+host+path); | |
post.addHeader("Content-Type", "application/x-www-form-urlencoded"); | |
post.setEntity(new StringEntity(body, "UTF-8")); | |
HttpResponse execute = client.execute(post); | |
int statusCode = execute.getStatusLine().getStatusCode(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(execute.getEntity().getContent())); | |
StringBuilder buf = new StringBuilder(); | |
String tmp = null; | |
while((tmp = br.readLine()) != null){ | |
buf.append(tmp); | |
} | |
System.out.println(statusCode); | |
System.out.println(buf.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment