分享
 
 
 

一个简单的网络客户端

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

A simple network client

/*

* Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.

*/

import Java.lang.*;

import java.io.*;

import java.util.*;

import javax.microedition.io.*;

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

/**

* A simple network client.

*

* This MIDlet shows a simple use of the HTTP

* networking interface. It opens a HTTP POST

* connection to a server, authenticates using

* HTTP Basic Authentication and writes a string

* to the connection. It then reads the

* connection and displays the results.

*/

public class NetClientMIDlet extends MIDlet

implements CommandListener {

private Display display; // handle to the display

private Form mainScr; // main screen

private Command cmdOK; // OK command

private Command cmdExit; // EXIT command

private Form dataScr; // for display of results

/**

* ConstrUCtor.

*

* Create main screen and commands. Main

* screen shows simple instructions.

*/

public NetClientMIDlet() {

display = Display.getDisplay(this);

mainScr = new Form("NetClient");

mainScr.append("Hit OK to make network connection");

mainScr.append(" ");

mainScr.append("Hit EXIT to quit");

cmdOK = new Command("OK", Command.OK, 1);

cmdExit = new Command("Exit", Command.EXIT, 1);

mainScr.addCommand(cmdOK);

mainScr.addCommand(cmdExit);

mainScr.setCommandListener(this);

}

/**

* Called by the system to start our MIDlet.

* Set main screen to be displayed.

*/

protected void startApp() {

display.setCurrent(mainScr);

}

/**

* Called by the system to pause our MIDlet.

* No actions required by our MIDLet.

*/

protected void pauseApp() {}

/**

* Called by the system to end our MIDlet.

* No actions required by our MIDLet.

*/

protected void destroyApp(boolean unconditional) {}

/**

* Gets data from server.

*

* Open a connection to the server, set a

* user and passWord to use, send data, then

* read the data from the server.

*/

private void genDataScr() {

ConnectionManager h = new ConnectionManager();

// Set message to send, user, password

h.setMsg("Esse quam videri");

h.setUser("book");

h.setPassword("bkpasswd");

byte[] data = h.Process();

// create data screen

dataScr = new Form("Data Screen");

dataScr.addCommand(cmdOK);

dataScr.addCommand(cmdExit);

dataScr.setCommandListener(this);

if (data == null data.length == 0) {

// tell user no data was returned

dataScr.append("No Data Returned!");

} else {

// loop trough data and extract strings

// (delimited by '\n' characters

StringBuffer sb = new StringBuffer();

for (int i = 0; i < data.length; i++) {

if (data[i] == (byte)'\n') {

dataScr.append(sb.toString());

sb.setLength(0);

} else {

sb.append((char)data[i]);

}

}

}

display.setCurrent(dataScr);

}

/**

* This method implements a state machine that drives

* the MIDlet from one state (screen) to the next.

*/

public void commandAction(Command c,

Displayable d) {

if (c == cmdOK) {

if (d == mainScr) {

genDataScr();

} else {

display.setCurrent(mainScr);

}

} else if (c == cmdExit) {

destroyApp(false);

notifyDestroyed();

}

}

}

/*

* Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.

*/

/**

* This class encodes a user name and password

* in the format (base 64) that HTTP Basic

* Authorization requires.

*/

class BasicAuth {

// make sure no one can instantiate this class

private BasicAuth() {}

// conversion table

private static byte[] cvtTable = {

(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E',

(byte)'F', (byte)'G', (byte)'H', (byte)'I', (byte)'J',

(byte)'K', (byte)'L', (byte)'M', (byte)'N', (byte)'O',

(byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T',

(byte)'U', (byte)'V', (byte)'W', (byte)'X', (byte)'Y',

(byte)'Z',

(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e',

(byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j',

(byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o',

(byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t',

(byte)'u', (byte)'v', (byte)'w', (byte)'x', (byte)'y',

(byte)'z',

(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4',

(byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9',

(byte)'+', (byte)'/'

};

/**

* Encode a name/password pair appropriate to

* use in an HTTP header for Basic Authentication.

* name the user's name

* passwd the user's password

* returns String the base64 encoded name:password

*/

static String encode(String name,

String passwd) {

byte input[] = (name + ":" + passwd).getBytes();

byte[] output = new byte[((input.length / 3) + 1) * 4];

int ridx = 0;

int chunk = 0;

/**

* Loop through input with 3-byte stride. For

* each 'chunk' of 3-bytes, create a 24-bit

* value, then extract four 6-bit indices.

* Use these indices to extract the base-64

* encoding for this 6-bit 'character'

*/

for (int i = 0; i < input.length; i += 3) {

int left = input.length - i;

// have at least three bytes of data left

if (left > 2) {

chunk = (input[i] << 16)

(input[i + 1] << 8)

input[i + 2];

output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];

output[ridx++] = cvtTable[(chunk&0x3F000) >>12];

output[ridx++] = cvtTable[(chunk&0xFC0) >> 6];

output[ridx++] = cvtTable[(chunk&0x3F)];

} else if (left == 2) {

// down to 2 bytes. pad with 1 '='

chunk = (input[i] << 16)

(input[i + 1] << 8);

output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];

output[ridx++] = cvtTable[(chunk&0x3F000) >>12];

output[ridx++] = cvtTable[(chunk&0xFC0) >> 6];

output[ridx++] = '=';

} else {

// down to 1 byte. pad with 2 '='

chunk = input[i] << 16;

output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];

output[ridx++] = cvtTable[(chunk&0x3F000) >>12];

output[ridx++] = '=';

output[ridx++] = '=';

}

}

return new String(output);

}

}

/*

* Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.

*/

/**

* Manages network connection.

*

* This class established an HTTP POST connection

* to a server defined by baseurl.

* It sets the following HTTP headers:

* User-Agent: to CLDC and MIDP version strings

* Accept-Language: to microedition.locale or

* to "en-US" if that is null

* Content-Length: to the length of msg

* Content-Type: to text/plain

* Authorization: to "Basic" + the base 64 encoding of

* user:password

*/

class ConnectionManager {

private HttpConnection con;

private InputStream is;

private OutputStream os;

private String ua;

private final String baseurl =

"http://127.0.0.1:8080/Book/netserver";

private String msg;

private String user;

private String password;

/**

* Set the message to send to the server

*/

void setMsg(String s) {

msg = s;

}

/**

* Set the user name to use to authenticate to server

*/

void setUser(String s) {

user = s;

}

/**

* Set the password to use to authenticate to server

*/

void setPassword(String s) {

password = s;

}

/**

* Open a connection to the server

*/

private void open() throws IOException {

int status = -1;

String url = baseurl;

String auth = null;

is = null;

os = null;

con = null;

// Loop until we get a connection (in case of redirects)

while (con == null) {

con = (HttpConnection)Connector.open(url);

con.setRequestMethod(HttpConnection.POST);

con.setRequestProperty("User-Agent", ua);

String locale =

System.getProperty("microedition.locale");

if (locale == null) {

locale = "en-US";

}

con.setRequestProperty("Accept-Language", locale);

con.setRequestProperty("Content-Length",

"" + msg.length());

con.setRequestProperty("Content-Type", "text/plain");

con.setRequestProperty("Accept", "text/plain");

if (user != null && password != null) {

con.setRequestProperty("Authorization",

"Basic " +

BasicAuth.encode(user,

password));

}

// Open connection and write message

os = con.openOutputStream();

os.write(msg.getBytes());

os.close();

os = null;

// check status code

status = con.getResponseCode();

switch (status) {

case HttpConnection.HTTP_OK:

// Success!

break;

case HttpConnection.HTTP_TEMP_REDIRECT:

case HttpConnection.HTTP_MOVED_TEMP:

case HttpConnection.HTTP_MOVED_PERM:

// Redirect: get the new location

url = con.getHeaderField("location");

os.close();

os = null;

con.close();

con = null;

break;

default:

// Error: throw exception

os.close();

con.close();

throw new IOException("Response status not OK:"+

status);

}

}

is = con.openInputStream();

}

/**

* Constructor

* Set up HTTP User-Agent header string to be the

* CLDC and MIDP version.

*/

ConnectionManager() {

ua = "Profile/" +

System.getProperty("microedition.profiles") +

" Configuration/" +

System.getProperty("microedition.configuration");

}

/**

* Process an HTTP connection request

*/

byte[] Process() {

byte[] data = null;

try {

open();

int n = (int)con.getLength();

if (n != 0) {

data = new byte[n];

int actual = is.read(data);

}

} catch (IOException ioe) {

} finally {

try {

if (con != null) {

con.close();

}

if (os != null) {

os.close();

}

if (is != null) {

is.close();

}

} catch (IOException ioe) {}

return data;

}

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有