|
前一段时间有人说自己碰到了http协议里的chunk问题,其实你自己实现chunk也可以,就是做个状态位的判断是了。本人实现过,在WTK虚拟机还没有公开的时候,但是wtk公开了你就用wtk里面的源马吧,写的很好,不过公开的代码里竟然少了行代码,这是在让我纳闷,不知道sun是故意的还是粗心,下面的代码增加了proxy支持,实在原有基础上改的,此代码已经应用很多应用,各位网友放心使用,有问题欢迎交流。 支持MIDP1.0和MIDP2.0,如果连接80或者8080端口,请申请证书,代码很多我给注释了,如果需要使用,请自行打开。 用法跟HttpConnection一样,只是在初始化的时候,我给修改成了openX,如果设置proxy,可以直接调用setProxyHost,注意的是,在放置url的时候不要忘记写好端口号。 SocketConnectionExt socktCon = SocketConnectionExt.openX(URL, Connector.READ_WRITE, true);
![]() ![]() import javax.microedition.io.StreamConnection;![]() /**//* * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. * Use is subject to license terms. */![]() import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.ByteArrayOutputStream;![]() import java.util.Hashtable; import java.util.Enumeration;![]() import javax.microedition.io.Connector;![]() ![]() /** *//** * This class implements the necessary functionality * for an HTTP connection. */![]() public class SocketConnectionExt...{ private int index; // used by URL parsing functions private String url; private String protocol; private String host; private String file; private String ref; private String query; private int port = 80; private int responseCode; private String responseMsg; private Hashtable reqProperties; private Hashtable headerFields; // private String[] headerFieldNames; // private String[] headerFieldValues; private String method; private int opens; private int mode; private String proxyHost; private boolean proxy = false; private boolean connected;![]() ![]() /**//* * In/out Streams used to buffer input and output */ private PrivateInputStream in; private PrivateOutputStream out;![]() ![]() /**//* * The data streams provided to the application. * They wrap up the in and out streams. */ private DataInputStream appDataIn; // private DataOutputStream appDataOut;![]() ![]() /**//* * The streams from the underlying socket connection. */ private StreamConnection streamConnnection; private DataOutputStream streamOutput; private DataInputStream streamInput;![]() ![]() /**//* * A shared temporary buffer used in a couple of places */ private StringBuffer stringbuffer; private String http_version = "HTTP/1.1";![]() ![]() /** *//** * Create a new instance of this class. * We are initially unconnected. */![]() public SocketConnectionExt() ...{ reqProperties = new Hashtable(); headerFields = new Hashtable(); stringbuffer = new StringBuffer(32); opens = 0; connected = false; method = "GET"; responseCode = -1; protocol = "http"; } ![]() public void setProxyHost(String proxyHost)...{ this.proxyHost = proxyHost; proxy = true; }![]() public void open(String url, int mode, boolean timeouts)![]() throws IOException ...{![]() ![]() if (opens > 0) ...{ throw new IOException("already connected"); }![]() opens++;![]() if (mode != Connector.READ && mode != Connector.WRITE![]() && mode != Connector.READ_WRITE) ...{ throw new IOException("illegal mode: " + mode); }![]() this.url = url; this.mode = mode;![]() parseURL(); } ![]() public static SocketConnectionExt openX(String url, int mode, boolean timeouts) throws IOException...{ SocketConnectionExt hcp = new SocketConnectionExt(); hcp.open(url, mode, timeouts); return hcp; }![]() ![]() public void close() throws IOException ...{![]() if (--opens == 0 && connected) ...{ disconnect(); } }![]() ![]() /**//* * Open the input stream if it has not already been opened. * @exception IOException is thrown if it has already been * opened. */![]() public InputStream openInputStream() throws IOException ...{![]() ![]() if (in != null) ...{ throw new IOException("already open"); }![]() // If the connection was opened and closed before the // data input stream is accessed, throw an IO exception![]() if (opens == 0) ...{ throw new IOException("connection is closed"); }![]() // Check that the connection was opened for reading![]() if (mode != Connector.READ && mode != Connector.READ_WRITE) ...{ throw new IOException("write-only connection"); }![]() connect(); opens++;![]() in = new PrivateInputStream(); return in; }![]() ![]() public DataInputStream openDataInputStream() throws IOException ...{![]() ![]() if (appDataIn != null) ...{ throw new IOException("already open"); }![]() // TBD: throw in exception if the connection has been closed.![]() if (in == null) ...{ openInputStream(); }![]() appDataIn = new DataInputStream(in); return appDataIn; }![]() ![]() public OutputStream openOutputStream() throws IOException ...{![]() ![]() if (mode != Connector.WRITE && mode != Connector.READ_WRITE) ...{ throw new IOException("read-only connection"); }![]() // If the connection was opened and closed before the // data output stream is accessed, throw an IO exception![]() if (opens == 0) ...{ throw new IOException("connection is closed"); }![]() ![]() if (out != null) ...{ throw new IOException("already open"); }![]() opens++; out = new PrivateOutputStream(); return out; }![]() ![]() public DataOutputStream openDataOutputStream() throws IOException ...{![]() ![]() if (mode != Connector.WRITE && mode != Connector.READ_WRITE) ...{ throw new IOException("read-only connection"); }![]() // If the connection was opened and closed before the // data output stream is accessed, throw an IO exception![]() if (opens == 0) ...{ throw new IOException("connection is closed"); }![]() ![]() if (out != null) ...{ throw new IOException("already open"); }![]() opens++; out = new PrivateOutputStream(); return new DataOutputStream(out); }![]() ![]() /** *//** * PrivateInputStream to handle chunking for HTTP/1.1. */![]() class PrivateInputStream extends InputStream ...{![]() int bytesleft; // Number of bytes left in current chunk int bytesread; // Number of bytes read since the stream was opened boolean chunked; // True if Transfer-Encoding: chunked boolean eof; // True if EOF seen![]() ![]() PrivateInputStream() throws IOException ...{ bytesleft = 0; bytesread = 0; chunked = false; eof = false;![]() // Determine if this is a chunked datatransfer and setup String te = (String)headerFields.get("transfer-encoding");![]() if (te != null && te.equals("chunked")) ...{ chunked = true; bytesleft = readChunkSize(); eof = (bytesleft == 0); } }![]() ![]() /** *//** |