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

📄 httpirc.java

📁 用java实现的红外线通讯源码
💻 JAVA
字号:
/*Copyright (C) 2004  Juho Vähä-HerttuaThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*/package jmirc;import java.io.*;import javax.microedition.io.*;import java.util.Vector;public class HttpIrc implements IrcConnection {	private Vector inqueue;	private String encoding, identifier, gwhost, gwpasswd, outbuf;	private int gwport;	private boolean connected, closeconn;	private int bytein;	private int byteout;	public HttpIrc(String gwhost, int gwport, String gwpasswd, String charset) {		this.gwhost = gwhost;		this.gwport = gwport;		this.gwpasswd = gwpasswd;		encoding = charset;		inqueue = new Vector();		outbuf = null;		bytein = 0;		byteout = 0;		connected = closeconn = false;		identifier = "";	}	public String connect(String host, int port, String init) {		HttpConnection c = null;		String url, ret = "";		int response;		url = "http://" + gwhost + ":" + gwport + "/connect?host=" + Utils.URLEncode(host, "US-ASCII") +		      "&port=" + Utils.URLEncode(""+port, "US-ASCII") + "&passwd=" + Utils.URLEncode(gwpasswd, "US-ASCII") +		      "&data=" + Utils.URLEncode(init, encoding);		try {			c = (HttpConnection) Connector.open(url);			c.setRequestMethod(HttpConnection.GET);			response = c.getResponseCode();			identifier = c.getHeaderField("X-Identifier");			if (c != null) c.close();		}		catch (IOException ioe) {			ret += "Error trying to connect to HTTP proxy server, aborting... ";			ret += "Exception: " + ioe.getMessage();			return ret;		}		if (response != HttpConnection.HTTP_OK) {			ret += "Error trying to connect to IRC server, reason: ";			switch(response) {				case HttpConnection.HTTP_FORBIDDEN:					ret += "Wrong password";					break;				case HttpConnection.HTTP_BAD_GATEWAY:					ret += "Bad gateway";					break;				case HttpConnection.HTTP_NOT_FOUND:					ret += "IRC connection not found";					break;				default:					ret += "HTTP response code: " + response;					break;			}			return ret;		}		else {			connected = true;			return null;		}	}	public void disconnect() {		if (connected) {			writeData("QUIT :used jmIrc\r\n");			closeconn = true;		}	}	public String readLine() {		String ret;		if (inqueue.size() > 0) {			ret = (String) inqueue.firstElement();			inqueue.removeElementAt(0);		}		else ret = "";		return ret;	}	public synchronized String updateConnection() {		String url, ret;		url = "http://" + gwhost + ":" + gwport + "/" + identifier;		if (outbuf != null) {			url += "?data=" + Utils.URLEncode(outbuf, encoding);			byteout += outbuf.getBytes().length;			outbuf = null;		}		ret = handleRequest(url, true);		if (closeconn) connected = false;		return ret;	}	public synchronized String writeData(String data) {		if (outbuf == null) outbuf = data;		else outbuf += data;		return null;	}	private String handleRequest(String url, boolean get) {		HttpConnection c = null;		InputStream is = null;		ByteArrayInputStream bais;		byte[] buf;		String temp, ret = "";		int response, len, i;		boolean encok;		try {			c = (HttpConnection) Connector.open(url);			if (get) c.setRequestMethod(HttpConnection.GET);			else c.setRequestMethod(HttpConnection.HEAD);			response = c.getResponseCode();			if (get) {				is = c.openInputStream();				// warning, loss of precision				len = (int) c.getLength();				if (len > 0) {					byte[] data = new byte[len];					for (i=0; i<len; i++) {						data[i] = (byte) is.read();					}					bytein += data.length;					try {						temp = new String(new byte[0], encoding);						encok = true;					} catch (UnsupportedEncodingException uee) {						encok = false;					}					bais = new ByteArrayInputStream(data);					while (bais.available() > 0) {						buf = Utils.readLine(bais);						if (buf != null) {							temp = encok?new String(buf, encoding):new String(buf);							inqueue.addElement(temp);						}					}				}			}			if (is != null) is.close();			if (c != null) c.close();		} catch (IOException ioe) {			ret += "Request failed, continuing...";			return ret;		}		if (response != 200) {			ret += "Error in connection to IRC server, aborting... ";			ret += "Error: HTTP response code: " + response;			connected = false;			return ret;		}		else return null;	}	public boolean hasDataInBuffer() {		if (inqueue.size() == 0) return false;		else return true;	}	public boolean isConnected() {		return connected;	}	public int getBytesIn() {		return bytein;	}	public int getBytesOut() {		return byteout;	}}

⌨️ 快捷键说明

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