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

📄 gateway.java

📁 蓝牙上网 j2me的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		if(HiisiMIDlet.settingForm.getUaId() == 0) {
			ua = HiisiMIDlet.hiisiMIDlet.getAppProperty("User-Agent");
			wap = HiisiMIDlet.hiisiMIDlet.getAppProperty("x-wap-profile");
		} else if(HiisiMIDlet.settingForm.getUaId() == 1) ua = HiisiMIDlet.hiisiMIDlet.getAppProperty("User-Agent2");
		else if(HiisiMIDlet.settingForm.getUaId() == 2) ua = HiisiMIDlet.hiisiMIDlet.getAppProperty("User-Agent3");
		
		bluetoothOutput.write((requestMethod + " " + requestURI + " " + requestProtocol + "\r\n").getBytes());
		HiisiMIDlet.logForm.addSentByte((requestMethod + " " + requestURI + " " + requestProtocol + "\r\n").length());
		Enumeration e = requestHeaderField.keys();
		String key;
		String value;
		while (e.hasMoreElements()){
			key = (String)e.nextElement();
			value = (String)requestHeaderField.get(key);
			if(!key.equals("user-agent") && !key.equals("x-wap-profile") && !key.equals("host") && !key.equals("connection")) {
				bluetoothOutput.write((key + ": " + value+"\r\n").getBytes());
				HiisiMIDlet.logForm.addSentByte((key + ": " + value+"\r\n").length());
			}
		}
		if(!ua.equals("")) {
			bluetoothOutput.write(("user-agent: " +  ua +"\r\n").getBytes());
			HiisiMIDlet.logForm.addSentByte(("user-agent: " +  ua +"\r\n").length());
		}
		if(!wap.equals("")) {
			bluetoothOutput.write(("x-wap-profile: " +  wap +"\r\n").getBytes());
			HiisiMIDlet.logForm.addSentByte(("x-wap-profile: " +  wap +"\r\n").length());
		}
		if(requestPort == 80) {
			bluetoothOutput.write(("host: " + requestHost + "\r\n").getBytes());
			HiisiMIDlet.logForm.addSentByte(("host: " + requestHost + "\r\n").length());
		} else {
			bluetoothOutput.write(("host: " + requestHost + ":" + requestPort + "\r\n").getBytes());
			HiisiMIDlet.logForm.addSentByte(("host: " + requestHost + ":" + requestPort + "\r\n").length());
		}
		bluetoothOutput.write("connection: close\r\n\r\n".getBytes());
		HiisiMIDlet.logForm.addSentByte("connection: close\r\n\r\n".length());
		if(requestMethod.equals("POST")) {
			bluetoothOutput.write(postData);
			postData = null;
		}
		bluetoothOutput.flush();
	}
	
	private void ConnectViaWap() throws IOException {
		String UA = HiisiMIDlet.hiisiMIDlet.getAppProperty("User-Agent") + "\n";
		String WAP = HiisiMIDlet.hiisiMIDlet.getAppProperty("x-wap-profile");
		
		hc.setRequestMethod(requestMethod);
		HiisiMIDlet.logForm.addSentByte((requestMethod + " " + requestURI + " " + requestProtocol + "\r\n").length());
		Enumeration e = requestHeaderField.keys();
		String key;
		String value;
		while (e.hasMoreElements()){
			key = (String)e.nextElement();
			value = (String)requestHeaderField.get(key);
			if(!key.equals("user-agent") && !key.equals("x-wap-profile") && !key.equals("host") && !key.equals("connection")) {
				hc.setRequestProperty(key, value);
				HiisiMIDlet.logForm.addSentByte((key + ": " + value + "\r\n").length());
			}
		}
		if(!UA.equals("")) {
			hc.setRequestProperty("user-agent", UA);
			HiisiMIDlet.logForm.addSentByte(("user-agent: " + UA + "\r\n").length());
		}
		if(!WAP.equals("")) {
			hc.setRequestProperty("x-wap-profile", WAP);
			HiisiMIDlet.logForm.addSentByte(("x-wap-profile: " + WAP + "\r\n").length());
		}
		if(requestPort == 80) {
			hc.setRequestProperty("host", requestHost);
			HiisiMIDlet.logForm.addSentByte(("host: " + requestHost + "\r\n").length());
		} else {
			hc.setRequestProperty("host", requestHost + ":" + requestPort);
			HiisiMIDlet.logForm.addSentByte(("host: " + requestHost + ":" + requestPort + "\r\n").length());
		}
		hc.setRequestProperty("connection", "close");
		HiisiMIDlet.logForm.addSentByte("connection: close\r\n\r\n".length());
		if(requestMethod.equals("POST")) {
			httpOutput = hc.openDataOutputStream();
			httpOutput.write(postData);
			HiisiMIDlet.logForm.addSentByte(postData.length);
			httpOutput.flush();
			postData = null;
		}
	}
	
	private void readResponseHeaderViaBluetooth() throws IOException, InterruptedException {
		StringBuffer sb = new StringBuffer();
		int c;
		int line = 0;
		while(true){
			if((c = readbuffer(bluetoothInput, 300)) == -1) {
				responseProtocol = "HTTP/1.1";
				responseCode = "504";
				responseMessage = "Gateway Time-out";
				break;
			} else {
				if((char)c == '\r') {
					if(sb.length() == 0) {
						break;
					} else {
						processResponseHeader(sb.toString(), line);
						sb.delete(0, sb.length());
						line++;
					}
				} else if((char)c == '\n') {
				} else {
					sb.append((char)c);
				}
			}
		}
		sb = null;
	}
	
	private void processResponseHeader(String str, int line) throws IOException {
		str = str.trim();
				
		if(line == 0){
			int delimitPos = str.indexOf(' ');
			responseProtocol = (str.substring(0, delimitPos)).trim();
			str = (str.substring(delimitPos + 1)).trim();
			delimitPos = str.indexOf(' ');
			responseCode = (str.substring(0, delimitPos)).trim();
			responseMessage = (str.substring(delimitPos + 1)).trim();
		} else {
			int delimitPos = str.indexOf(':');
			String key = ((str.substring(0, delimitPos)).trim()).toLowerCase();
			String value = (str.substring(delimitPos + 1)).trim();
			responseHeaderField.put(key, value);
		}
	}
	
	private void writeResponseFromBluetooth() throws IOException, InterruptedException {
		output.write((responseProtocol + " " + responseCode + " " + responseMessage + "\r\n").getBytes());
		HiisiMIDlet.logForm.addRecvdByte((responseProtocol + " " + responseCode + " " + responseMessage + "\r\n").length());
		Enumeration e = responseHeaderField.keys();
		String key;
		String value;
		while (e.hasMoreElements()){
			key = (String)e.nextElement();
			value = (String)responseHeaderField.get(key);
			if(!key.equals("connection")) {
				output.write((key + ": " + value+"\r\n").getBytes());
				HiisiMIDlet.logForm.addRecvdByte((key + ": " + value+"\r\n").length());
			}
		}
		output.write("connection: close\r\n\r\n".getBytes());
		HiisiMIDlet.logForm.addRecvdByte("connection: close\r\n\r\n".length());
		
		if(!requestMethod.equals("HEAD")) {
			int dummy;
			if((dummy = readbuffer(bluetoothInput,100)) == -1) return;
			
			int b;
			if(responseHeaderField.get("content-length") != null) {
				int contentLength = Integer.valueOf((String)responseHeaderField.get("content-length")).intValue();
				int temp = 0;
				int read = 0 ;
				byte[] buf = new byte[4096];
				while ((read < contentLength) && (temp != -1)) {
					temp = readbuffer(bluetoothInput, buf, 4096, 300);
					if(temp != -1) {
						output.write(buf, 0, temp);
						read += temp;
						byteTranfered += temp;
						HiisiMIDlet.logForm.addRecvdByte(temp);
					}
				}
				buf = null;
			} else {
				byte[] buf = new byte[4096];
				while(true) {
					if((b = readbuffer(bluetoothInput, buf, 4096, 5)) == -1) {
						break;
					} else {
						output.write(buf, 0, b);
						byteTranfered += b;
						HiisiMIDlet.logForm.addRecvdByte(b);
					}
				}
				buf = null;
			}
		}
		output.flush();
	}
	
	private void readResponseHeaderViaWap() throws IOException {
		responseProtocol = "HTTP/1.1";
		responseCode = Integer.toString(hc.getResponseCode());
		responseMessage = hc.getResponseMessage();
		String key = "";
		for(int i = 0; ((key = hc.getHeaderFieldKey(i)) != null); i++){
			responseHeaderField.put(key, hc.getHeaderField(i));
		}
	}
	
	private void writeResponseFromWap() throws IOException {
		output.write((responseProtocol + " " + responseCode + " " + responseMessage + "\r\n").getBytes());
		HiisiMIDlet.logForm.addRecvdByte((responseProtocol + " " + responseCode + " " + responseMessage + "\r\n").length());
		Enumeration e = responseHeaderField.keys();
		String key;
		String value;
		while (e.hasMoreElements()){
			key = (String)e.nextElement();
			value = (String)responseHeaderField.get(key);
			if(!key.equals("connection")) {
				output.write((key + ": " + value+"\r\n").getBytes());
				HiisiMIDlet.logForm.addRecvdByte((key + ": " + value+"\r\n").length());
			}
		}
		output.write("connection: close\r\n\r\n".getBytes());
		HiisiMIDlet.logForm.addRecvdByte("connection: close\r\n\r\n".length());
		
		int contentLength = (int)hc.getLength();
		int temp = 0;
		int read = 0 ;
		byte[] buf = new byte[4096];
		while ((read < contentLength) && (temp != -1)) {
			temp = httpInput.read(buf, 0, 4096);
			if(temp != -1) {
				output.write(buf, 0, temp);
				read += temp;
				byteTranfered += temp;
				HiisiMIDlet.logForm.addRecvdByte(temp);
			}
		}
		output.flush();
		buf = null;
	}
	
	private String getAccessTime() {
		cal = Calendar.getInstance();
		year = cal.get(Calendar.YEAR);
		switch(cal.get(Calendar.MONTH) + 1) {
			case 1: month = "Jan"; break;
			case 2: month = "Feb"; break;
			case 3: month = "Mar"; break;
			case 4: month = "Apr"; break;
			case 5: month = "May"; break;
			case 6: month = "Jun"; break;
			case 7: month = "Jul"; break;
			case 8: month = "Aug"; break;
			case 9: month = "Sep"; break;
			case 10: month = "Oct"; break;
			case 11: month = "Nov"; break;
			case 12: month = "Dec"; break;
		}
		day_of_month = cal.get(Calendar.DAY_OF_MONTH);
		hour = cal.get(Calendar.HOUR_OF_DAY);
		minute = cal.get(Calendar.MINUTE);
		second = cal.get(Calendar.SECOND);
		return formatNum(day_of_month) + "/" + month + "/" + year +
			":" + formatNum(hour) + ":" + formatNum(minute) + ":" + formatNum(second) + " +0900";
	}
	
	private String formatNum(int num) {
		if(num < 10) return "0" + num;
		else return "" + num;
	}
	
	private static int readbuffer(DataInputStream stream,  int reread) throws IOException, InterruptedException {
		int c = -1;
		for(int i = 0; i < reread; i++) {
				if(stream.available() != 0) {
					c = stream.read();
					break;
				}
				Thread.sleep(100);
			}
		return c;
	}
	
	private static int readbuffer(DataInputStream stream,  byte[] buf, int len, int reread) throws IOException, InterruptedException {
		int c = -1;
		for(int i = 0; i < reread; i++) {
				if(stream.available() != 0) {
					c = stream.read(buf, 0, len);
					break;
				}
				Thread.sleep(100);
			}
		return c;
	}
	
	void checkConnection() {
		Thread check = new Thread() {
			public void run() {
				try {
					Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
					HiisiMIDlet.mainForm.log("Checking bluetooth connection...");
					bc = (StreamConnection)Connector.open(HiisiMIDlet.bluetoothConnection.getUrl());
					bluetoothOutput = bc.openDataOutputStream();
					bluetoothOutput.write("\r".getBytes());
					HiisiMIDlet.mainForm.log("Bluetooth connection is available.\nHiisi Proxy "
						+ HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
						+ " is idling...");
					if(bc != null) {
						bc.close();
						bc = null;
					}
					if(bluetoothOutput != null) {
						bluetoothOutput.close();
						bluetoothOutput = null;
					}
				} catch(IOException e) {
					HiisiMIDlet.mainForm.log("Bluetooth connection is not available.\nHiisi Proxy "
						+ HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
						+ " is idling...");
				}
			}
		};
		check.start();
	}
}

⌨️ 快捷键说明

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