/**
* JavaHttpsUrlConnectionReader
*
*
*@author Palaniappan S
*/
import java.io.*;
import java.net.*;
import java.security.cert.X509Certificate;
import java.util.Properties;
import javax.net.ssl.*;
public class JavaHttpsUrlConnectionReader implements javax.net.ssl.X509TrustManager {
public static void main(String[] args) throws Exception {
String output = new JavaHttpsUrlConnectionReader().doHttpsUrlConnectionAction("https://somesecuresite.com");
System.out.println("Output : "+output);
}
public String doHttpsUrlConnectionAction(String desiredUrl) throws Exception {
URL url;
int responseCode=0;
StringBuffer responseData = new StringBuffer( );
//Uncomment below lines of code if any proxy enabled in the network you are using
//Properties props = System.getProperties();
//props.put("https.proxyHost", "127.0.0.1");
//props.put("https.proxyPort", "8080");
// ###########
SSLContext sc = SSLContext.getInstance("SSLv3");
TrustManager[] tma = { new JavaHttpsUrlConnectionReader() };
sc.init(null, tma, null);
SSLSocketFactory ssf = sc.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(ssf);
// ###########
HttpsURLConnection connection = null;
String nurl = desiredUrl;
HostnameVerifier hv = new HostnameVerifier()
{
public boolean verify(String urlHostName, SSLSession session)
{
System.out.println("Warning: URL Host: " + urlHostName + " vs. "
+ session.getPeerHost());
return true;
}
};
// Now you are telling the JRE to trust any https server.
// If you know the URL that you are connecting to then this should not be a problem
// trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
try {
url = new URL(nurl);
connection = (HttpsURLConnection) url.openConnection();
System.out.println("Conn established "+connection);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
responseCode = connection.getResponseCode();
System.out.println("response code : "+responseCode);
connection.connect();
} catch(Exception e) {
System.err.println(e);
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String _ResData = null;
while ((_ResData = reader.readLine()) != null)
{
responseData.append(_ResData);
//_ResData = sckStream.readLine();
}
System.out.println("Received Data: "+ responseData.toString());
} catch (Exception e) {
System.err.println(e);
}
finally
{
// close the reader; this can throw an exception too, so
// wrap it in another try/catch block.
if (reader != null)
{
try
{
connection.disconnect();
reader.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
return responseData.toString();
}
// TrustManager Methods
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
Hope the above code helped you......
Best Regards,
Palaniappan
Friday, May 21, 2010
Thursday, April 22, 2010
How to store/retrieve UTF8 data into MySQL table
Steps to be followed :
1. Putty-->change settings-->Translation-->UTF-8
2. CREATE DATABASE IF NOT EXISTS DBNAME charset='utf8';
3. set names 'utf8'; in mysql prompt or through application/script
4. Create table and load data.No need to set charset as UTF8 for table or fields since database is already set as UTF8.
5. In hibernate.properties file include the following :
"hibernate.connection.useUnicode=true
hibernate.connection.characterEncoding=UTF-8"
while retrieving data from DB through Application "set names utf-8" should be executed to communicate the DB server that transaction of data will be in utf8 format
6. response.setContentType("text/html; charset=UTF-8");
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"), true);
response.setCharacterEncoding("UTF-8");
7.This step is not mandatory try if others were not successful
Configure tomact server.xml with the following
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
8.Try the following if java standalone application is developed :
class test{
public static void main(String[] args){
System.out.println("\u0939\u0947\u0932\u094D\u0932\u094A \u0935\u094A\u0930\u094D\u0932\u094D\u0926\u094D");
}
}
If you use eclipse ide follow these steps before executing the above code.
Rightclick on the code-->select Run Configurations-->common tab-->console encoding-->other-->select UTF-8
Run the application you will find "हेल्लॊ वॊर्ल्द्"
I feel this article helped you.
Thanks.
1. Putty-->change settings-->Translation-->UTF-8
2. CREATE DATABASE IF NOT EXISTS DBNAME charset='utf8';
3. set names 'utf8'; in mysql prompt or through application/script
4. Create table and load data.No need to set charset as UTF8 for table or fields since database is already set as UTF8.
5. In hibernate.properties file include the following :
"hibernate.connection.useUnicode=true
hibernate.connection.characterEncoding=UTF-8"
while retrieving data from DB through Application "set names utf-8" should be executed to communicate the DB server that transaction of data will be in utf8 format
6. response.setContentType("text/html; charset=UTF-8");
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"), true);
response.setCharacterEncoding("UTF-8");
7.This step is not mandatory try if others were not successful
Configure tomact server.xml with the following
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
8.Try the following if java standalone application is developed :
class test{
public static void main(String[] args){
System.out.println("\u0939\u0947\u0932\u094D\u0932\u094A \u0935\u094A\u0930\u094D\u0932\u094D\u0926\u094D");
}
}
If you use eclipse ide follow these steps before executing the above code.
Rightclick on the code-->select Run Configurations-->common tab-->console encoding-->other-->select UTF-8
Run the application you will find "हेल्लॊ वॊर्ल्द्"
I feel this article helped you.
Thanks.
Monday, February 15, 2010
Tuesday, June 9, 2009
Subscribe to:
Posts (Atom)