📄 html_httpconnectionhandlerimpl.java
字号:
package cn.com.javachen;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.io.HttpConnection;
/**
* http连接处理器实现
*
* @author cleverpig
*
*/
public class HTML_HttpConnectionHandlerImpl implements HttpConnectionHandler {
private String message = "";
public HTML_HttpConnectionHandlerImpl() {
}
public boolean putRequestHeaderProperty(HttpConnection conn, String key,
String keyValue) {
message = "";
try {
conn.setRequestProperty(key, keyValue);
return true;
} catch (Exception ex) {
message = ex.getMessage();
}
return false;
}
public String getResponseHeaderProperty(HttpConnection conn, String key) {
return conn.getRequestProperty(key);
}
public boolean putRequestHeader(HttpConnection conn,
Hashtable propertiesPair) {
Enumeration keyEnumer = propertiesPair.keys();
boolean result = true;
while (keyEnumer.hasMoreElements()) {
String keyName = (String) keyEnumer.nextElement();
String keyValue = (String) propertiesPair.get(keyName);
if (putRequestHeaderProperty(conn, keyName, keyValue) == false) {
result = false;
}
}
return result;
}
public boolean setRequestMethod(HttpConnection conn, String methodName) {
message = "";
try {
conn.setRequestMethod(methodName);
return true;
} catch (Exception ex) {
message = ex.getMessage();
return false;
}
}
public Hashtable getResponseHeader(HttpConnection conn,
String[] propertyNames) {
Hashtable result = new Hashtable();
for (int i = 0; i < propertyNames.length; i++) {
String keyValue = conn.getRequestProperty(propertyNames[i]);
result.put(propertyNames[i], keyValue);
}
return result;
}
public boolean sendRequestData(HttpConnection conn, Object sendData) {
message = "";
try {
DataOutputStream os = conn.openDataOutputStream();
os.writeUTF((String) (sendData));
os.flush();
return true;
} catch (Exception ex) {
message = ex.getMessage();
return false;
}
}
public Object getResponseData(HttpConnection conn) {
DataInputStream is = null;
// String content = "";
// String result = "";
// InputStream isStrm = null;
// try {
// isStrm = conn.openInputStream();
// } catch (IOException e) {
// // TODO 自动生成 catch 块
// e.printStackTrace();
// }
// InputStreamReader isr = new InputStreamReader(isStrm);
// try {
// try {
// isr = new InputStreamReader(isStrm, "UTF8");
// } catch (Exception e1) {
// try {
// isr = new InputStreamReader(isStrm, "UTF-8");
// } catch (Exception e2) {
// try {
// isr = new InputStreamReader(isStrm, "utf8");
// } catch (Exception e3) {
// try {
// isr = new InputStreamReader(isStrm, "utf-8");
// } catch (UnsupportedEncodingException e) {
// // TODO 自动生成 catch 块
// e.printStackTrace();
// }
// }
// }
// }
// int ic;
// StringBuffer b = new StringBuffer();
// while ((ic = isr.read()) != -1) {
// b.append((char) ic);
// }
// content = b.toString();
// // byte[] gbk = content.getBytes("utf-8");
// // for (int i = 0; i < gbk.length; i++)
// // System.out.print(Integer.toHexString((gbk[i])) + " ");
// // System.out.print(content);
// // result=new String(content.getBytes("utf-8"),"utf-8");
// isr.close();
// } catch (Exception e) {
// // TODO: handle exception
// e.printStackTrace();
// }
message = "";
try {
is = conn.openDataInputStream();
} catch (Exception ex) {
message = ex.getMessage();
return null;
}
byte[] data = null;
String type = getResponseHeaderProperty(conn, RSH_CONTENT_TYPE);
int len = 0;
try {
len = Integer.parseInt(getResponseHeaderProperty(conn,
RSH_CONTENT_LENGTH));
} catch (Exception ex) {
len = 0;
}
if (len > 0) {
int actual = 0;
int bytesread = 0;
data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
try {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
} catch (Exception ex) {
message = ex.getMessage();
return null;
}
}
} else {
int ch;
Vector vbuffer = new Vector();
try {
while ((ch = is.read()) != -1) {
vbuffer.addElement(new Integer(ch));
}
} catch (Exception ex) {
message = ex.getMessage();
return null;
}
len = vbuffer.size();
data = new byte[len];
for (int i = 0; i < len; i++) {
data[i] = ((Integer) vbuffer.elementAt(i)).byteValue();
}
}
String result="";
String content="";
try {
result = new String(data,"utf-8");
int flagBeginPosition = result.indexOf("charset=");
int flagEndPosition = result.indexOf("\">", flagBeginPosition);
if (flagEndPosition > flagBeginPosition) {
type = result.substring(flagBeginPosition + "charset=".length(),
flagEndPosition);
}
System.out.println("获取的html字符集:" + type);
content = new String(result.getBytes("utf-8"), "utf-8");
if (type != null) {
try {
content = new String(result.getBytes("utf-8"), "utf-8");
} catch (Exception ex) {
message = ex.getMessage();
}
}
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
// return result;
return content;
}
public String getMessage() {
return message;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -