亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

一個簡單的網絡客戶端

2019-11-18 16:17:45
字體:
來源:轉載
供稿:網友

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.49028c.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
伊人久久久久久久久久| 精品伊人久久97| 国产精品老女人视频| 亚洲一区国产精品| 国产成人精品午夜| 欧美一乱一性一交一视频| 啪一啪鲁一鲁2019在线视频| 国产成人精品日本亚洲专区61| 国产视频久久久久久久| 成人羞羞国产免费| 亚洲成人网久久久| 国产精品国语对白| 国产黑人绿帽在线第一区| 成人黄色av播放免费| 欧美性猛交xxx| 日韩资源在线观看| 91欧美日韩一区| 日韩中文在线不卡| 国产91精品青草社区| 全球成人中文在线| 亚洲国产精品va在线看黑人动漫| 91禁国产网站| 伊人亚洲福利一区二区三区| 日日骚久久av| 国产精品老牛影院在线观看| 国产福利视频一区二区| 夜夜嗨av一区二区三区四区| 国产成人午夜视频网址| 亚洲综合成人婷婷小说| 久久久久久久亚洲精品| 成人春色激情网| 国产精品一区二区三区久久久| 精品国产一区二区在线| 成人黄色激情网| 亚洲欧美中文日韩在线v日本| 欧美插天视频在线播放| 欧美日韩免费在线| 欧美成人第一页| 欧美视频在线观看免费网址| www.欧美免费| 日韩av第一页| 7m第一福利500精品视频| 在线播放国产一区中文字幕剧情欧美| 在线性视频日韩欧美| 亚洲aaaaaa| 国产精品高潮呻吟视频| 精品美女永久免费视频| 日韩精品高清视频| 日韩欧美国产网站| 欧美激情一级二级| 91精品国产沙发| 中文国产亚洲喷潮| 欧美激情亚洲精品| 国产99久久精品一区二区 夜夜躁日日躁| 中文字幕日韩精品在线观看| 国产视频综合在线| 久久免费国产视频| 亚洲最大的免费| 欧美怡红院视频一区二区三区| 国产精品久久久久久久久久新婚| 97碰在线观看| 日韩中文字幕国产| 久久精品色欧美aⅴ一区二区| 欧美国产日韩免费| 国产91色在线播放| 在线看片第一页欧美| 国产精品视频男人的天堂| 成人字幕网zmw| 亚洲曰本av电影| 97精品国产97久久久久久春色| 国产精品狠色婷| 国产精品久久中文| 欧洲成人免费视频| 2019日本中文字幕| 午夜精品久久久久久久久久久久久| 亚洲国产精品人久久电影| 久久夜精品va视频免费观看| 欧美午夜精品在线| 中文字幕在线成人| 国产精品高潮粉嫩av| 中日韩美女免费视频网站在线观看| 欧美性20hd另类| 国产视频精品久久久| 国产日韩精品在线播放| 亚洲欧洲日产国产网站| 日韩精品极品视频免费观看| 欧美精品videosex牲欧美| 日韩精品中文字幕在线观看| 亚洲国产97在线精品一区| 国产欧美日韩中文字幕在线| 亚洲va欧美va国产综合剧情| 国产精品日韩欧美| 欧美日韩国产影院| 日产日韩在线亚洲欧美| 亚洲色图五月天| 日韩国产精品一区| 亚洲成人黄色网| 热99精品只有里视频精品| 欧美丝袜美女中出在线| 亚洲在线第一页| 亚洲美女性生活视频| 欧美在线视频免费| 欧美日韩精品在线播放| 日韩小视频在线| 综合激情国产一区| 国产ts一区二区| 久久精品亚洲一区| 国外成人在线视频| 欧美小视频在线| 亚洲色图av在线| 成人动漫网站在线观看| 日韩欧美一区二区在线| 91色精品视频在线| 成人国产在线激情| 奇米影视亚洲狠狠色| 亚洲精品福利免费在线观看| 欧美精品少妇videofree| 91免费的视频在线播放| 亚洲加勒比久久88色综合| 欧美高清无遮挡| 欧美成人精品h版在线观看| 国产精品第一页在线| 欧美日韩亚洲一区二区| 日本在线精品视频| 色综合久久久久久中文网| 亚洲xxxx在线| 欧美久久精品午夜青青大伊人| 51色欧美片视频在线观看| 精品久久久视频| 日韩欧美在线观看视频| 国产精品va在线| 久久男人资源视频| 欧美成人第一页| 国产精品极品美女粉嫩高清在线| 91精品久久久久久久久青青| 亚洲精品720p| 久久躁狠狠躁夜夜爽| 大伊人狠狠躁夜夜躁av一区| 久久成人在线视频| 久久男人的天堂| 久久99精品久久久久久琪琪| 亚洲а∨天堂久久精品喷水| 精品中文字幕久久久久久| 欧美性高潮床叫视频| 国产精品一区二区性色av| 亚洲电影第1页| 欧美性xxxxhd| 国模精品一区二区三区色天香| 黑人巨大精品欧美一区二区一视频| 国产精品青草久久久久福利99| 日本成熟性欧美| 国产在线久久久| 欧美精品成人在线| 国产精品日韩在线一区| 青草青草久热精品视频在线网站| 久久精品视频中文字幕| 欧美丝袜一区二区三区| 亚洲欧美日韩中文在线制服| 色多多国产成人永久免费网站| 国产精品高潮呻吟久久av黑人| 国产一区二区三区视频免费| 久久亚洲精品中文字幕冲田杏梨| 欧美日韩国产91| 亚洲国产私拍精品国模在线观看|