Send and Receive Data From and To HTTPS Server


  1. Ravi_Aegis

    Ravi_Aegis New Member

    Hi All,

    I am Ravi.

    I have created an Simple Program, goal of that program is to make connection to HTTPS server and send data to https server and receive data from https server.

    in that case i am successful to make a connection to Https server and also able to read data what server sends to client, but when from my client program i am trying to send data to Https server that time there is no error but in server when i try to read that data then there is no data available in Server input stream to read.

    This is my Code of client,

    package com.mobhype.sdk;

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io_OutputStreamWriter;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;

    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;


    import android.app.Activity;
    import android.content.Intent;

    public class MobHypeSDK {

    public String responseString;
    public String responseURL;
    String url;
    MultiFormatReader multiFormatReader;
    RGBMonochromeBitmapSource source;
    Result r;
    String decryptedText;

    public void sendImageToHTTPS(String path)
    {
    //String url = "https://localhost:8443/MobHypeServer?actionType=sendImage";

    try
    {
    String cuki=new String();

    System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
    System.setProperty("javax.net.ssl.trustStore","keystore");

    TrustManager easyTrustManager = new X509TrustManager()
    {
    public java.security.cert.X509Certificate[] getAcceptedIssuers()
    {
    return null;
    }
    public void checkClientTrusted(
    java.security.cert.X509Certificate[] chain, String authType)
    throws java.security.cert.CertificateException {
    // TODO Auto-generated method stub
    }
    public void checkServerTrusted(
    java.security.cert.X509Certificate[] chain, String authType)
    throws java.security.cert.CertificateException {
    // TODO Auto-generated method stub
    }
    };

    URL url = new URL("https://localhost:8443/MobHypeServer?actionType=sendImage");
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
    HttpsURLConnection shuc = (HttpsURLConnection) url.openConnection();
    System.out.println("Connection Code--------->" + shuc.getResponseCode());
    shuc.setDoInput(true);
    shuc.setDoOutput(true);
    shuc.setFollowRedirects(true);
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str = in.readLine();
    while(str != null)
    {
    System.out.println("DATA From Https Server------>" + str);
    str = in.readLine();
    }
    OutputStreamWriter out = new OutputStreamWriter(shuc.getOutputStream());

    out.write("OUT WriteFirst.....");
    out.write("OUT WriteFirst.....");
    out.flush(); out.close();
    System.out.println("Data Are Written Successfully.......");
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    This above is Client Code,

    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 doPost(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();
    }
    }
    }


    please any idea where i am doing wrong in this code.

    all ideas are welcome
    Thank you
    Ravi:)

    Advertisement

Share This Page