⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpsimpleconnection.java

📁 j2me下的1套UI框架.包含j2me开发中会应用的各种低级组件
💻 JAVA
字号:
package com.jmobilecore.comm;

import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Enumeration;

public class HttpSimpleConnection {

    public HttpConnection http;
    public DataInputStream iStrm;
    public Hashtable httpProperties = new Hashtable(8);
    public int responseCode = HttpConnection.HTTP_OK;

    public HttpSimpleConnection() {
        init();
    }

    public HttpSimpleConnection(String connString) {
        init();
        open(connString);
    }

    protected void init() {
        http = null;
        iStrm = null;
        httpProperties.put("Content-Type", "application/x-www-form-urlencoded");
    }

    public boolean open(String connString) {
        boolean bRes = openHTTP(connString);
        if (!bRes) {
            closeHTTP();
        }
        return bRes;
    }

    public void close() {
        if (iStrm != null) closeInputStream();
        if (http != null) closeHTTP();
    }

    protected boolean openHTTP(String url) {
        try {
            http = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
            http.setRequestMethod(HttpConnection.POST);
            for (Enumeration e=httpProperties.keys(); e.hasMoreElements();) {
                String key = (String) e.nextElement();
                String value = (String) httpProperties.get(key);
                http.setRequestProperty(key, value);
            }
            return true;
        } catch (Exception ignored) {
            return false;
        }
    }

    protected void closeHTTP() {

        if (http != null) {
            try {
                http.close();
            } catch (Exception ignored) {
            }
            http = null;
        }
    }

    protected boolean openInputStream() {

        try {
            iStrm = http.openDataInputStream();
            responseCode = http.getResponseCode();
            if (responseCode != HttpConnection.HTTP_OK) {
                return false;
            }
            return true;
        } catch (Exception ignored) {
            return false;
        }
    }

    protected void closeInputStream() {

        if (iStrm != null) {
            try {
                iStrm.close();
            } catch (Exception ignored) {
            }
            iStrm = null;
        }
    }

    public int readBytes(byte[] data, int len) throws IOException {
        if (iStrm == null) openInputStream();
        return iStrm.read(data, 0, len);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -