Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Send Data From Android Application to HTTPS Server

843811Apr 8 2010 — edited Apr 9 2010
Hi All,

I am Ravi, i am Working on one Android Application in that i want to send data from Android Application to Server Through HTTPS Protocol.
in that i can read data that Server send to client in that i am successful.
But when i trying to send data from Client to Https Server that time i am not able to receive that data at Server End.
i post my Application Code here,

This is Android Application Code,

import java.io.File;
import com.mobhype.sdk.MobHypeSDK;
public class QRCodeReader extends Activity {
String path;
EditText text;
String responseUrl;
WebView mWebView;
String decryptedURL;
MobHypeSDK mobHypeSDK = new MobHypeSDK();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button decodeButton = (Button)findViewById(R.id.button);
final Button encodeButton = (Button)findViewById(R.id.encode);
final Button verifyButton = (Button)findViewById(R.id.verify);
final Button sendImageButton = (Button)findViewById(R.id.sendImage);
final TextView content = (TextView)findViewById(R.id.content);
String photoPath = "/data/anr/photo.jpg";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=2;
Bitmap bm = BitmapFactory.decodeFile(photoPath, options);
sendImageButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
String path = "D:\\test.jpg";
mobHypeSDK.sendImageToHTTPS(path);
}
});
}
}

This is my Client Code, This class function i call from Android Application

public class MobHypeSDK {

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

try
{

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 is My Server Code,

public class MobHypeServer extends HttpServlet
{

protected void doGett(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
response.setContentType("text/html");
String actionType = request.getParameter("actionType");
System.out.println("Action Type's value....."+actionType);
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeChars("First DATA From Server");
oos.writeChars("Second DATA From Server");
oos.writeChars("Thirs DATA From Server");
oos.flush();oos.close();
DataInputStream input = new DataInputStream(request.getInputStream());
String str="";
while (null != ((str = input.readLine()))){
System.out.println("STR--------->" + str);
if (str.length() >0){
str = str.trim();
if(!str.equals("")){
System.out.println(str);
//str += str + "\n";
}
}
}

input.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Please let know that where i am going wrong in this.
All ideas are welcome
Thankyou
Ravi

Edited by: Ravi_Aegis on Apr 8, 2010 12:55 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 6 2010
Added on Apr 8 2010
7 comments
861 views