| 网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 |
![]() |
| Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道 |
前一段时间有人说自己碰到了http协议里的chunk问题,其实你自己实现chunk也可以,就是做个状态位的判断是了。本人实现过,在WTK虚拟机还没有公开的时候,但是wtk公开了你就用wtk里面的源马吧,写的很好,不过公开的代码里竟然少了行代码,这是在让我纳闷,不知道sun是故意的还是粗心,下面的代码增加了proxy支持,实在原有基础上改的,此代码已经应用很多应用,各位网友放心使用,有问题欢迎交流。 网管u家u.bitscn@com
支持MIDP1.0和MIDP2.0,如果连接80或者8080端口,请申请证书,代码很多我给注释了,如果需要使用,请自行打开。
网管网www_bitscn_com
用法跟HttpConnection一样,只是在初始化的时候,我给修改成了openX,如果设置proxy,可以直接调用setProxyHost,注意的是,在放置url的时候不要忘记写好端口号。
中国网管论坛bbs.bitsCN.com
SocketConnectionExt socktCon = SocketConnectionExt.openX(URL, Connector.READ_WRITE, true);
System.out.println("URL: " + mURL);
//socktCon.open(mURL, Connector.READ_WRITE, true);
if(useProxy) socktCon.setProxyHost("10.0.0.172:80"); 中国网管论坛bbs.bitsCN.com


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 网管u家u.bitsCN.com
* 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; 网管联盟bitsCN_com 
import java.util.Hashtable;
import java.util.Enumeration;
import javax.microedition.io.Connector;

/** *//** 网管朋友网www_bitscn_net
* 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; 网管有家bitscn.net
private String host;
private String file;
private String ref;
private String query;
private int port = 80; 网管bitscn_com
private int responseCode;
private String responseMsg;
private Hashtable reqProperties;
private Hashtable headerFields; 网管论坛bbs_bitsCN_com
// private String[] headerFieldNames;
// private String[] headerFieldValues;
private String method;
private int opens; 网管下载dl.bitscn.com
private int mode;
private String proxyHost;
private boolean proxy = false; 中国网管论坛bbs.bitsCN.com
private boolean connected;

/**//* 网管u家bitscn.net
* In/out Streams used to buffer input and output
*/
private PrivateInputStream in;
private PrivateOutputStream out;

/**//* 网管朋友网www_bitscn_net
* The data streams provided to the application.
* They wrap up the in and out streams.
*/
private DataInputStream appDataIn;
// private DataOutputStream appDataOut; 网管bitscn_com


/**//* 网管下载dl.bitscn.com
* The streams from the underlying socket connection.
*/
private StreamConnection streamConnnection;
private DataOutputStream streamOutput;
private DataInputStream streamInput; 网管u家bitscn.net 

/**//*
* A shared temporary buffer used in a couple of places
*/
private StringBuffer stringbuffer;
private String http_version = "HTTP/1.1"; 

/** *//** 中国网管论坛bbs.bitsCN.com
* Create a new instance of this class.
* We are initially unconnected.
*/
public SocketConnectionExt() ...{ 网管u家u.bitscn@com
reqProperties = new Hashtable();
headerFields = new Hashtable();
stringbuffer = new StringBuffer(32);
opens = 0;
connected = false;
method = "GET"; 中国网管联盟bitsCN.com
responseCode = -1;
protocol = "http";
} 网管有家bitscn.net

public void setProxyHost(String proxyHost)...{ 网管u家bitscn.net
this.proxyHost = proxyHost;
proxy = true;
}
public void open(String url, int mode, boolean timeouts) 网管有家bitscn.net 
throws IOException ...{ 网管朋友网www_bitscn_net 

if (opens > 0) ...{ 网管联盟bitsCN_com
throw new IOException("already connected");
}
opens++; 中国网管论坛bbs.bitsCN.com

if (mode != Connector.READ && mode != Connector.WRITE
&& mode != Connector.READ_WRITE) ...{
throw new IOException("illegal mode: " + mode);
}
this.url = url; 网管下载dl.bitscn.com
this.mode = mode;
parseURL();
}

public static SocketConnectionExt openX(String url, int mode, boolean timeouts) throws IOException...{ 网管bitscn_com
SocketConnectionExt hcp = new SocketConnectionExt();
hcp.open(url, mode, timeouts);
return hcp;
}
网管联盟bitsCN@com 
public void close() throws IOException ...{ 网管u家www.bitscn.net 
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) ...{ 网管u家u.bitsCN.com
throw new IOException("already open");
}
// If the connection was opened and closed before the 中国网管论坛bbs.bitsCN.com
// 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) ...{ 网管有家bitscn.net
throw new IOException("write-only connection");
}
connect();
opens++;
in = new PrivateInputStream();
return in;
} 网管联盟bitsCN@com 

public DataInputStream openDataInputStream() throws IOException ...{ 网管u家u.bitscn@com 

if (appDataIn != null) ...{ 网管网www_bitscn_com
throw new IOException("already open");
}
// TBD: throw in exception if the connection has been closed. 网管u家bitscn.net 
if (in == null) ...{
openInputStream();
}
appDataIn = new DataInputStream(in);
return appDataIn;
}

public OutputStream openOutputStream() throws IOException ...{ 网管u家www.bitscn.net 

if (mode != Connector.WRITE && mode != Connector.READ_WRITE) ...{
throw new IOException("read-only connection");
}
// If the connection was opened and closed before the 网管朋友网www_bitscn_net
// data output stream is accessed, throw an IO exception
if (opens == 0) ...{ 网管朋友网www_bitscn_net
throw new IOException("connection is closed");
}

if (out != null) ...{ 网管有家bitscn.net
throw new IOException("already open");
}
opens++;
out = new PrivateOutputStream();
return out;
}

public DataOutputStream openDataOutputStream() throws IOException ...{ 网管网www.bitscn.com 

if (mode != Connector.WRITE && mode != Connector.READ_WRITE) ...{ 网管bitscn_com
throw new IOException("read-only connection");
}
// If the connection was opened and closed before the 网管网www_bitscn_com
// data output stream is accessed, throw an IO exception
if (opens == 0) ...{ 网管论坛bbs_bitsCN_com
throw new IOException("connection is closed");
}

if (out != null) ...{
throw new IOException("already open");
}
opens++;
out = new PrivateOutputStream();
return new DataOutputStream(out);
}

/** *//** 网管u家u.bitsCN.com
* PrivateInputStream to handle chunking for HTTP/1.1.
*/
class PrivateInputStream extends InputStream ...{ 网管u家www.bitscn.net

int bytesleft; // Number of bytes left in current chunk
int bytesread; // Number of bytes read since the stream was opened 网管u家u.bitsCN.com
boolean chunked; // True if Transfer-Encoding: chunked
boolean eof; // True if EOF seen 网管朋友网www_bitscn_net 

PrivateInputStream() throws IOException ...{ 网管联盟bitsCN@com
bytesleft = 0;
bytesread = 0;
chunked = false;
eof = false;
// Determine if this is a chunked datatransfer and setup
String te = (String)headerFields.get("transfer-encoding"); 网管u家u.bitsCN.com 
if (te != null && te.equals("chunked")) ...{ 网管联盟bitsCN@com
chunked = true;
bytesleft = readChunkSize();
eof = (bytesleft == 0); 网管有家bitscn.net
}
}

/** *//**
* Returns the number of bytes that can be read (or skipped over)
* from this input stream without blocking by the next caller of
* a method for this input stream.
*
* This method simply returns the number of bytes left from a 网管有家www.bitscn.net
* chunked response from an HTTP 1.1 server.
*/
public int available() throws IOException ...{ 

if (connected) ...{
return bytesleft ;
} else ...{ 网管bitscn_com
throw new IOException("connection is not open");
}
} 网管u家www.bitscn.net 

/** *//**
* Reads the next byte of data from the input stream.
* The value byte is returned as an <code>int</code> in
* the range <code>0</code> to <code>255</code>.
* If no byte is available because the end of the stream
* has been reached, the value <code>-1</code> is returned. 中国网管联盟bitsCN.com
* This method blocks until input data is available, the
* end of the stream is detected, or an exception is thrown.
*
* <p> A subclass must provide an implementation of this method.
*
* @return the next byte of data, or <code>-1</code>
* if the end of the stream is reached.
* @exception IOException if an I/O error occurs.