IT tutorials
 
Mobile
 

Java ME on Symbian OS : Networking and General Connection Framework (part 2) - Security Policy for Network Connections & NetworkDemo MIDlet

11/5/2011 3:23:33 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

5. Security Policy for Network Connections

The connections discussed above are part of the Net Access function group (see the RSP addendum to the MIDP specification). On the Nokia N95, for example, an untrusted MIDlet can access the Net Access function group with User permission (explicit confirmation required from the user). Figure 1 shows an example of an unsigned MIDlet, Google's Gmail, and the available permission options on a Nokia N95. This policy varies from licensee to licensee so you must check with the manufacturer of your target devices which settings apply for existing security domains.

Figure 1. Permission options for an unsigned MIDlet on a Nokia N95

6. NetworkDemo MIDlet

We finish this section with a simple example using MIDP's javax.microedition.io.HttpConnection to interrogate a web server. The NetworkDemo MIDlet connects to a web server via HttpConnection, and reads and displays all the headers and the first 256 characters of the response itself. Let's take a look at the core network functionality exploited in this MIDlet. The connection work is all done in the ClientConnection class:

public class ClientConnection extends Thread {
private static final int TEXT_FIELD_SIZE = 256;
private NetworkDemoMIDlet midlet = null;
private String url = null;
public ClientConnection(NetworkDemoMIDlet midlet) {
this.midlet = midlet;
}
public void sendMessage(String url) {
this.url = url;
start();
}
public void run() {
try{
HttpConnection conn = (HttpConnection)Connector.open(url);
int responseCode = conn.getResponseCode();

midlet.append("Response code","" + responseCode);
int i = 0;
String headerKey = null;
while((headerKey = conn.getHeaderFieldKey(i++)) != null) {
midlet.append(headerKey,conn.getHeaderField(headerKey));
}

InputStream in = conn.openInputStream();
StringBuffer buffer = new StringBuffer(TEXT_FIELD_SIZE);
int ch;
int read = 0;

while ( (ch = in.read()) != −1 && read < TEXT_FIELD_SIZE) {
buffer.append((char)ch);
read++;
}

midlet.append("HTML Response" ,buffer.toString());
conn.close();
conn = null;
}
catch(Exception e){
e.printStackTrace();
}
}
}


The url parameter of the sendMessage() method has the following form:

http://www.symbian.com:80

The sendMessage() method creates a request and then starts a new Thread to create the connection, send the request and read the response. Let us look at the contents of the thread's run() method in more detail:

HttpConnection conn = (HttpConnection)Connector.open(url);
int responseCode = conn.getResponseCode();
midlet.append("Response code","" + responseCode);
int i = 0;

String headerKey = null;
while((headerKey = conn.getHeaderFieldKey(i++)) != null) {
midlet.append(headerKey,conn.getHeaderField(headerKey));
}

An HttpConnection is opened using the URI given by the user and is used first to retrieve the HTTP response code (200, in case of success). We also loop through the HTTP response methods until they are all read and append them to the MIDlet's Form object as a StringItem.

Having read the HTTP readers, it is time to read the actual HTML content. We open an InputStream from the HttpConnection, start reading the input and save it to a StringBuffer of TEXT_FIELD_SIZE length. Once we reach this value in number of bytes read, we leave the loop, as we don't want to pollute the user's screen with too much raw HTML.

InputStream in = conn.openInputStream();
StringBuffer buffer = new StringBuffer(TEXT_FIELD_SIZE);
int ch;
int read = 0;
while ( (ch = in.read()) != −1 && read < TEXT_FIELD_SIZE) {
buffer.append((char)ch);
read++;
}

After reading TEXT_FIELD_SIZE bytes of HTML, we append them to the MIDlet's Form so they can be shown on the screen along with all the other information we retrieved. Figure 2 shows the NetworkDemo MIDlet running on a Nokia N95.

Figure 2. NetworkDemo MIDlet running on a Nokia N95: a) connecting to the URL, b) receiving the headers, and c) reading the HTML
 
Others
 
- Java ME on Symbian OS : Networking and General Connection Framework (part 1)
- jQuery 1.3 : Effects - Basic hide and show
- jQuery 1.3 : Effects - Inline CSS modification
- Mobile Web Apps : Events
- Mobile Web Apps : Setting up Shop
- iPhone 3D Programming : Simplify with glDrawTexOES
- iOS SDK : Adding a SQLite Database
- The Anatomy of a Mobile Site : Navigation and menu systems (part 3) - Paving Mobile Pathways & Switcher Links
- The Anatomy of a Mobile Site : Navigation and menu systems (part 2) - Breadcrumbs, Header and Footer Navigation
- The Anatomy of a Mobile Site : Navigation and menu systems (part 1) - Navigational Lists & Decorating Menus
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us