Hi All,
I am Ravi. i am working on an application in that i am trying to send data to Https Server and Receive data from HTTPS Server. Through HTTPS Protocol.
in that i am successful to receive data from HTTPS Server which Server send to client.
but when i am trying to send data from client to server that time data are written successfully to Server outputstream but in Server when i am trying to read that data there is no data available to read from Server inputstream.
i post code of my whole application.
This is My Client code From where i connect to server and trying to send data to server.
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class TestHttpsConnection {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
String str = subscribe("","","","","","");
System.out.println("DATA RETURN-------->" + str);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static String subscribe(String dist,String userid,String password,String email,String name,String expirydate) throws Exception{
String resp = "";
// String urlString="https://<localhost>/";
String urlString="https://localhost:8443/MobHypeServer?actionType=sendImage";
URL url;
HttpsURLConnection urlConn;
OutputStreamWriter printout;
DataInputStream input;
String str = "";
int flag=1;
try {
Properties sysProperties = System.getProperties();
// change proxy settings if required and enable the below lines
// sysProperties.put("proxyHost", "proxy.starhub.net.sg");
// sysProperties.put("proxyPort", "8080");
// sysProperties.put("proxySet", "true");
// Now you are telling the JRE to ignore the hostname
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
// TODO Auto-generated method stub
System.out.println("Warning: URL Host: " + hostname + " vs. "
+ session.getPeerHost());
return true;
}
};
{
/*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);
url = new URL(urlString);
urlConn =(HttpsURLConnection) url.openConnection();
urlConn.setDoInput(true);
Object object;
urlConn.setUseCaches(false);
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
printout = new OutputStreamWriter(urlConn.getOutputStream());
printout.write("This is Testing Data only....");
printout.write("Data are Written Successfully.....");
printout.flush();printout.close();
System.out.println("End Writting Data-------->");
input = new DataInputStream(urlConn.getInputStream());
while (null != ((str = input.readLine()))){
if (str.length() >0){
str = str.trim();
if(!str.equals("")){
//System.out.println(str);
resp += str;
}
}
}
input.close();
System.out.println("<-----END PRG------>");
}catch(MalformedURLException mue){ mue.printStackTrace();}
catch(IOException ioe){ ioe.printStackTrace();}
return resp;
}
// Just add these two functions in your program
public static class miTM implements javax.net.ssl.TrustManager,
javax.net.ssl.X509TrustManager
{
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
public boolean isServerTrusted(
java.security.cert.X509Certificate[] certs)
{
return true;
}
public boolean isClientTrusted(
java.security.cert.X509Certificate[] certs)
{
return true;
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException
{
return;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException
{
return;
}
}
private static void trustAllHttpsCertificates() throws Exception
{
// Create a trust manager that does not validate certificate chains:
javax.net.ssl.TrustManager[] trustAllCerts =
new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc =
javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(
sc.getSocketFactory());
}
}
This is My Server Code,
package com.mobhype.server;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MobHypeServer
*/
public class MobHypeServer extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
response.setContentType("text/html");
String actionType = request.getParameter("actionType");
System.out.println("This is DO POST METHOD............");
System.out.println("Action Type's value....."+actionType);
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject("First DATA From Server");
oos.writeObject("Second DATA From Server");
oos.writeObject("Thirs DATA From Server");
oos.flush();oos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
This is the Code,
Please let me know where i am going wring in this.
All Idea are welcome.
Thank you
Ravi

