PC HELP Forum  

Go Back   PC HELP Forum > PCHELP Forum > Mobilephones & Handhelds

Reply
 
Thread Tools Display Modes
  #1  
Old 07-14-2009, 03:08 AM
mixdev's Avatar
mixdev mixdev is offline
Administrator
 
Join Date: Jul 2009
Location: Bangalore
Posts: 33
Send a message via Yahoo to mixdev Send a message via Skype™ to mixdev
Default [J2ME] SSL connections to http/mail servers using Java

I want to connect to mail(pop/imap) and/or HTTP servers over secure sockets layer. I am seeing multiple options to do so. Anybody ventured into this? Whats the best way to do this in a J2me mobile app? Any low level explanation will be helpful.

Some options
1) https
2) javax.microedition.io.secureconnection
Reply With Quote
  #2  
Old 07-20-2009, 02:43 AM
mixdev's Avatar
mixdev mixdev is offline
Administrator
 
Join Date: Jul 2009
Location: Bangalore
Posts: 33
Send a message via Yahoo to mixdev Send a message via Skype™ to mixdev
Default j2me secure socket connection code

Here is a solution from the Corej2ME book (thanks to @daaku for the lead). Although this is not a complete code, this can pretty much be the starting point of a https based connection in J2ME
Code:
/*--------------------------------------------------
* ViewFile.java
*
* Send client request (method, header, body)
* Get server response (status, header, body)
*
* Example from the book:     Core J2ME Technology
* Copyright John W. Muchow   http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;

public class ViewFile extends MIDlet
{
private String url = "http://www.corej2me.com/midpbook_v1e1/ch14/getHeaderInfo.txt";

public void startApp()
{
try
{
processRequest();
}
catch (Exception e)
{
System.err.println("Msg: " + e.toString());
}
}    

private void processRequest() throws IOException
{
HttpConnection http = null;
InputStream iStrm = null;

try
{
// Create the connection
http = (HttpConnection) Connector.open(url);

//----------------
// Client Request
//----------------
// 1) Send request method
http.setRequestMethod(HttpConnection.GET);

// 2) Send header information (this header is optional)
http.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
//      http.setRequestProperty("If-Modified-Since", "Mon, 16 Jul 2001 22:54:26 GMT");

// If you experience IO problems, try 
// removing the comment from the following line
//http.setRequestProperty("Connection", "close");      

// 3) Send body/data - No data for this request
Reply With Quote
  #3  
Old 07-20-2009, 02:47 AM
mixdev's Avatar
mixdev mixdev is offline
Administrator
 
Join Date: Jul 2009
Location: Bangalore
Posts: 33
Send a message via Yahoo to mixdev Send a message via Skype™ to mixdev
Default Cont'd

Code:
// Server Response
//----------------
System.out.println("url: " + url);
System.out.println("-------------------------");      

// 1) Get status Line
System.out.println("Msg: " + http.getResponseMessage());                  
System.out.println("Code: " + http.getResponseCode());                

// 2) Get header information 
if (http.getResponseCode() == HttpConnection.HTTP_OK)
{
System.out.println("field 0: " + http.getHeaderField(0));        
System.out.println("field 1: " + http.getHeaderField(1));
System.out.println("field 2: " + http.getHeaderField(2));        
System.out.println("-------------------------");
System.out.println("key 0: " + http.getHeaderFieldKey(0));        
System.out.println("key 1 : " + http.getHeaderFieldKey(1));        
System.out.println("key 2: " + http.getHeaderFieldKey(2));                
System.out.println("-------------------------");
System.out.println("content: " + http.getHeaderField("content-type"));
System.out.println("date: " + http.getHeaderField("date"));
System.out.println("last-modified: " + http.getHeaderField("last-modified"));                

// 3) Get data (show the file contents)
String str;
iStrm = http.openInputStream();
int length = (int) http.getLength();
if (length != -1)
{
// Read data in one chunk
byte serverData[] = new byte[length];
iStrm.read(serverData);
str = new String(serverData);
}
else  // Length not available...
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();       

int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);

str = new String(bStrm.toByteArray());
bStrm.close();                
}

System.out.println("File Contents: " + str);

//-----------------------------
// Show connection information
//-----------------------------
System.out.println("Host: " + http.getHost());
System.out.println("Port: " + http.getPort());
System.out.println("Type: " + http.getType());                

}
}catch(Exception e){
e.printStackTrace();

}finally{
// Clean up
if (iStrm != null)
iStrm.close();
if (http != null)
http.close();
}
}

public void pauseApp(){}

public void destroyApp(boolean unconditional){ }
}




Pretty bad formatting. Sorry for it
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
Forum Jump


All times are GMT +5.5. The time now is 03:33 PM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Copyright © 2009 PCHELP.in. Hosted by HostMe