CMWAP(通过WAP网关代理,由WAP网关连接服务器,然后将服务器返回的结果转发给客户端)和CMNET(使用直接连接互联网的方式与服务器通信)是中国移动提供的两种网络接入方式,CMWAP实际上是一种客户端 <--> WAP网关 <--> 服务器的连接方式,而CMNET采用的是客户端 <--> 服务器直连方式。
public class ActionDispatcher extends MIDlet {
private Display display = Display.getDisplay(this);
private Form f;
protected void startApp() throws MIDletStateChangeException {
f = new Form("Network Test");
f.append("正在连接网络, 情稍候;当出现cmwap和cmnet字样时,程序结束,可退出");
display.setCurrent(f);
try {
cmwap(); // 电脑模拟器无法访问wap网络,除非电脑能够访问的中国移动的wap网关。这个要通过手机测试。
cmnet();
} catch (IOException e) {
e.printStackTrace();
}
f.append("程序运行结束,可退出");
display.setCurrent(f);
}
private void cmwap() throws IOException {
HttpConnection conn = null;
InputStream is = null;
if (f.size() > 0) f.delete(0); // cmwap()是先执行的,后执行的cmnet()不必执行Form.delete()方法
try {
conn = (HttpConnection) Connector.open("http://10.0.0.172:80/", Connector.READ_WRITE, true);
conn.setRequestProperty("X-Online-Host", "wap.sina.com");
// conn.setRequestProperty("Accept", "*/*");
is = conn.openInputStream();
f.append("cmwap: no exception, test passed");
} catch (Exception e) {
f.append("cmwap failed: " + e.toString());
} finally {
if (is != null) {
is.close();
is = null;
}
if (conn != null) {
conn.close();
conn = null;
}
f.append("\n");
}
}
private void cmnet() throws IOException {
HttpConnection conn = null;
InputStream is = null;
try {
conn = (HttpConnection) Connector.open("http://wap.sina.com/", Connector.READ_WRITE, true); // timeouts - A flag to indicate that the caller wants timeout exceptions
is = conn.openInputStream();
f.append("cmnet: no exception, test passed");
} catch (Exception e) {
f.append("cmnet failed: " + e.toString());
} finally {
if (is != null) {
is.close();
is = null;
}
if (conn != null) {
conn.close();
conn = null;
}
f.append("\n");
}
}
protected void pauseApp() {}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
}
经由CMWAP方式接入时,MIDlet直接和WAP网关建立连接,地址是10.0.0.172。同时,MIDlet应该在Http头(Header)中添加下面的信息,X-Online-Host:联网服务器的域名和端口,这样WAP网关作为代理向服务器转发请求。同时可以在HTTP头中添加一些字段,例如User Agent等。
再发一个Demo
public HttpConnection openCMWAP(String url, String contentType) throws IOException {
HttpConnection httpConn = null;
for (int i = 0; i < 5; i++) { // 连接5次,如果5次都不成功,说明网络or服务器有问题
if (httpConn != null) httpConn.close();
httpConn = (HttpConnection) Connector.open(url, Connector.READ);
httpConn.setRequestMethod(HttpConnection.GET);
if (httpConn.getHeaderField("Content-Type").indexOf(contentType) > -1) {
if (httpConn.getResponseCode() == HttpConnection.HTTP_OK) {
return httpConn;
}
break;
}
}
throw new IOException("Max Connector open count.");
}
public HttpConnection openCMNET(String url, String contentType) throws IOException {
String proxy = "http://10.0.0.172";
String serverName = null;
boolean needProxy = false;
if (proxy != null && proxy.length() > 0) {
needProxy = true;
// http://servername/filename
serverName = url.substring(0, (url.indexOf("/", 7) + 1));
url = proxy + "/" + url.substring(url.indexOf("/", 7) + 1, url.length());
}
HttpConnection httpConn = null;
for (int i = 0; i < 5; i++) {
if (httpConn != null) httpConn.close();
httpConn = (HttpConnection) Connector.open(url, Connector.READ, true);
if (needProxy) {
httpConn.setRequestProperty("X-Online-Host", serverName);
httpConn.setRequestProperty("Accept", "*/*");
}
httpConn.setRequestMethod(HttpConnection.GET);
if (httpConn.getHeaderField("Content-Type").indexOf(contentType) > -1) {
if (httpConn.getResponseCode() == HttpConnection.HTTP_OK) {
return httpConn;
}
break;
}
}
throw new IOException("Max Connector open count.");
}