📄 httpthread.java.svn-base
字号:
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import java.io.*;
import java.util.*;
public class HttpThread implements Runnable
{
public static final int MAX_REDIRECTS = 5;
Thread m_thread;
//App app;
Vector m_sendList;
DataListener m_dataListener;
boolean isProxy;
public Hashtable m_head=new Hashtable();
public HttpThread(boolean isProxy )
{
this.isProxy = isProxy;
m_sendList = new Vector();
}
/*
* 这是中国移动的WAP网关代理地址
*/
//#if vasproject.http
private final static String CMCC_GATEWAY = "http://10.0.0.172";
//#endif
public void setDataListener( DataListener listener )
{
this.m_dataListener = listener;
}
public HttpConnection open(String url) throws Exception {
//#if vasproject.http
HttpConnection connection = null;;
String sServerIp;
// System.out.println("cnnMode:" + app.getCnnMode() );
if( isProxy ){
int nPosIndex;
if(url.startsWith("http://"))
{
nPosIndex = url.substring(7, url.length()).indexOf('/') + 7;
sServerIp = url.substring(7, nPosIndex);
}
else
{
nPosIndex = url.indexOf('/');
sServerIp = url.substring(0, nPosIndex);
}
String sSuffix = url.substring(nPosIndex);
String sRequestURL = CMCC_GATEWAY + sSuffix;
connection = (HttpConnection) Connector.open(sRequestURL,Connector.READ_WRITE,true );
connection.setRequestProperty("X-Online-Host", sServerIp);
}else{
connection = (HttpConnection) Connector.open(url,Connector.READ_WRITE,true );
}
return connection;
}
public void run()
{
boolean sending = false;
while ( m_thread != null ) {
/*synchronized (app) {
try {
app.wait();
} catch (InterruptedException e) {
//System.out.println("waiting error");
//e.printStackTrace();
}
}*/
try
{
if (!m_sendList.isEmpty() && !sending ) {
sending = true;
String buf = (String) m_sendList.elementAt(0);
int index = buf.indexOf( "^" );
String url = buf.substring( 0,index );
buf = buf.substring( index+1 );
index = buf.indexOf( "^" );
String pdata = buf.substring( 0,index );
String cmd = buf.substring( index+1 );
if ( pdata != null && pdata.equals( "" ) )
pdata = null;
byte[] result = httpGetOrPost( url,pdata,null );
System.out.println( new String( result ) );
if ( m_dataListener != null )
{
if ( result != null )
{
m_dataListener.doInfo( cmd,result );
}
else
m_dataListener.doError( cmd,"Receive Data is null" );
}
m_sendList.removeElementAt(0);
m_sendList.trimToSize();
Thread.yield();
sending = false;
}
else
{
try
{
Thread.sleep(100);
}
catch( Exception e )
{}
}
}
catch( Exception e )
{
sending = false;
if ( m_dataListener != null )
m_dataListener.doError("exception","in run http thread!" + e.toString() );
m_sendList.removeElementAt(0);
m_sendList.trimToSize();
}
try{
Thread.sleep(100);
}catch( Exception e ){}
}
}
private byte[] httpGetOrPost (String url, String postData, String encoding) throws IOException {
HttpConnection c = null;
DataInputStream is = null;
DataOutputStream os = null;
byte[] data = null;
try {
int status = -1;
// If there are more than n redirects, give up, we might
// be in a loop.
int redirects = 0;
try
{
c = open(url);
//c.setRequestMethod(HttpConnection.GET);
}
catch( Exception e )
{
if ( m_dataListener != null )
m_dataListener.doError( "connect","Connect error[" + status + "]");
}
if ( c == null ) return null;
if (postData != null) {
c.setRequestMethod(HttpConnection.POST);
byte[] b = null;
try {
if (encoding != null) {
b = postData.getBytes(encoding);
} else {
b = postData.getBytes();
}
} catch (java.io.UnsupportedEncodingException e) {
b = postData.getBytes();
}
c.setRequestProperty("Content-Type","application/octet-stream");
// c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
c.setRequestProperty("Connection", "keep-alive");
//c.setRequestProperty("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
//c.setRequestProperty("Accept-Charset", "UTF-8, ISO-8859-1, GB2312");
//c.setRequestProperty("Accept-Encoding", "*");
//c.setRequestProperty("Accept-Language", "en-us, zh-cn");
c.setRequestProperty("Content-Length", String.valueOf(b.length));
//c.setRequestProperty("User-Agent","Java/1.5.0_08" );
os = c.openDataOutputStream();
//Sos.write(b);
for( int i=0;i<b.length; i++)
os.writeByte(b[i]);
// try{
/// os.close();
//}catch(Exception e){
//}
//os.write(b, 0, b.length );
// os.flush();
//System.out.println("psot: finish" );
}
status = c.getResponseCode();
System.out.println( "status:" + status );
// Only HTTP_OK (200) means the content is returned.
if ( status != HttpConnection.HTTP_OK ) {
if ( m_dataListener != null )
m_dataListener.doError( "status",status + "<Response error!>" );
return null;
}
//App.m_errorInfo.addElement( "before open input stream" );
// open the InputStream
try
{
is = c.openDataInputStream();
}
catch( Exception e )
{
if ( m_dataListener != null )
m_dataListener.doError( "openstream","Open Input Stream error!" );
return null;
}
// Report the ContentLength
long len = c.getLength();
System.out.println("Content-Length: " + len);
// Avoid reading to the end of a stream if you know
// how many bytes you're going to get. The penalties
// are severe.
if (len > 0) {
data = new byte[(int)len];
try {
int actual = 0;
int bytesread = 0 ;
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, (int) len - bytesread);
bytesread += actual;
}
return data;
} catch (IOException e) {
if ( m_dataListener != null )
m_dataListener.doError( "read","Read Data error!"+e.toString() );
return null;
}
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
baos.write((byte)ch);
}
return baos.toByteArray();
}
}
finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (c != null) {
try{
// +++ This seems to be a bug with the SUN MIDP
// emulator.. If we DO close the connection here, then
// the next time we attempt to open a connection to the same
// host, we get an error "could not reconnect to server". If
// I comment out the line below, the error doesn't happen.
c.close();
}catch(Exception e){
}
}
}
//return null;
}
//返回请求编号
public void httpPost (String cmd,String url, String postData) {
//return httpGetOrPost(url, postData, null);
//int no = nextNo();
StringBuffer buf = new StringBuffer();
buf.append( url );
buf.append( "^" );
buf.append( postData );
buf.append( "^" );
buf.append( cmd );
m_sendList.addElement( buf.toString() );
}
//返回请求编号
public void httpGet (String cmd,String url) {
//return httpGetOrPost(url, null, null);
//int no = nextNo();
StringBuffer buf = new StringBuffer();
buf.append( url );
buf.append( "^^" );
buf.append( cmd );
m_sendList.addElement( buf.toString() );
}
public void startThread()
{
if( m_thread == null )
{
m_thread =new Thread( this );
m_thread.start();
}
}
public void stopThread()
{
m_thread = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -