一:比较Http get和post区别:
java解释,和j2me类似,详细出处参考:http://www.jb51.net/web/12714.html还有字符编码的应用,有时间要学学java web,对服务器请求数据有更深了解
1、Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据。
2、Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而各个变量之间使用“&”连接;比http://211.139.191.144:8008/taac/services/applyCopyrightDeclaration.action?username=ldh&password=zhm,Post是将表单中的数据放在form的数据体中按照变量和值相对应的方式,传递到action所指向URL。如:设置属性值
Enumeration perpertyKeys = requestProperty.keys();
while (perpertyKeys.hasMoreElements()) {
String key = (String) perpertyKeys.nextElement();
String value = (String) requestProperty.get(key);
conn.setRequestProperty(key, value);
}
3、Get是不安全的,因为在传输过程,数据被放在请求的URL中,而如今现有的很多服务器、代理服务器或者用户代理都会将请求URL记录到日志文件中,然后放在某个地方,这样就可能会有一些隐私的信息被第三方看到。另外,用户也可以在浏览器上直接看到提交的数据,一些系统内部消息将会一同显示在用户面前。Post的所有操作对用户来说都是不可见的。
4、Get传输的数据量小,这主要是因为受URL长度限制最多1024字节;而Post可以传输大量的数据,所以在上传文件只能使用Post(当然还有一个原因,将在后面的提到)。
5、Get限制Form表单的数据集的值必须为ASCII字符;而Post支持整个ISO10646字符集。默认是用ISO-8859-1编码
6、Get是Form的默认方法。
二:构建服务器,目的是搞个测试地址测试get,post请求
1.下载APMServ5.2.6,phpcms_v9_beta_UTF8
2.安装好APMServ5.2.6后,把phpcms_v9_beta_UTF8解压到APMServ5.2.6安装目录./www/htdocs/下,打开 APMServ5.2.6启动服务,运行http://127.0.0.1出现画面,提示你要安装phpcms程序点击“请点这里开始安装”,按提示一步一步往下操作,到填写数据库信息时,数据库服务器用默认的,数据库帐号:root,密码为空,数据库名称你自己取,数据库前缀用默认为可以了,你也可以修改为自己想要的,然后填写网站管理员用户名、密码和邮箱即可,其它的用默认即可,点击下一步; 安装进行中,请稍等,大约几十秒后,提示安装完成即可。
在开启APMServ下,http://localhost:80/或者http://127.0.0.1:80是服务器地址,现在可以在./www/htdocs/下放文件如1.txt,hai7.png,访问地址是http://localhost:80/1.txt和http://localhost:80/hai7.png,在网络中是什么地址,好像还好做一些配置,以后再研究
也可参考APMServ官网使用教程http://www.cnzz.cc/edu/Server/web/4190.html
三:get访问,post访问,cmwap设置见下面:
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* 测试get,post请求,需要开启自己设定的APMServ服务才能测试,
* 在APMServ安装目录./www/htdocs/下放icon.png,hai7.wav,1.txt资源
*
* @author hsz
*/
public class TestGetPost extends MIDlet implements CommandListener {
public HttpConnection conn;
public MainThread mainThread;
public TextField user;// 用户名
public TextField pass;// 密码
public Command c1;
private Form f;
private int http_type = TYPE_POST;
public static final int TYPE_GET = 0;
public static final int TYPE_POST = TYPE_GET + 1;
public static final String URL = "http://localhost:80/";
public static final int TYPE_IMG = 0;// 图片
public static final int TYPE_AUDOI = 1;// 音乐
public static final int TYPE_TXT = 2;// TXT
public static final boolean ISUSE_CMWAP = false;
public static final String CMWAP_IP = "http://10.0.0.172:80/";
public TestGetPost() {
f = new Form("测试get,post!");
c1 = new Command("确定", Command.SCREEN, 1);
f.addCommand(c1);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
mainThread = new MainThread();
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
}
public void commandAction(Command c, Displayable dd) {
new Thread(mainThread).start();
}
class MainThread implements Runnable {
public void run() {
System.out.println("start");
switch (http_type) {
case TYPE_GET:
get(getUrl(TYPE_IMG), TYPE_IMG);
break;
case TYPE_POST:
post(getUrl(TYPE_IMG), TYPE_IMG);
break;
default:
break;
}
}
}
public String getUrl(int type) {
String tem = null;
if (type == TYPE_IMG) {
tem = URL + "icon2.png";
} else if (type == TYPE_AUDOI) {
tem = URL + "hai7.wav";
} else if (type == TYPE_TXT) {
tem = URL + "1.txt";
}
return tem;
}
public void displayResult(InputStream is, int type) {
try {
switch (type) {
case TYPE_IMG:// 从网络中获取图片并显示
Image im = Image.createImage(is);// 该方法为MIDP 2.0方法
f.append(im);
System.out.println("获得图片");
break;
case TYPE_AUDOI:// 从网络中获取音乐并播放,播放有问题,没有去更正
Player p;
p = Manager.createPlayer(is, "audio/x-wav");
p.start();
System.out.println("开始播放音乐");
break;
case TYPE_TXT:// 从网络中获取文本并显示
byte[] buffer = new byte[1024 * 4];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = 0;
while ((i = is.read(buffer)) != -1) {
baos.write(buffer, 0, i);
}
byte[] allData = baos.toByteArray();
String str = new String(allData);
System.out.println("get获取的数据:" + str);
f.append(str);
buffer = null;
allData = null;
baos.close();
break;
default:
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (MediaException e) {
e.printStackTrace();
}
}
/**
* 进行cmwap设置,把host:port换成CMWAP_IP,并设置"X-Online-Host"为Host:Port
*
* @param url
* @return HttpConnection 已经打开URL的连接
*/
public HttpConnection openConnUrl(String url) {
try {
if (ISUSE_CMWAP) {// 进行cmwap设置,把host:port换成CMWAP_IP,并设置"X-Online-Host"为Host:Port
HttpURLParam urlparam = new HttpURLParam(url);
String CMWAPurl = CMWAP_IP + urlparam.getPath();
conn = (HttpConnection) Connector.open(CMWAPurl);
conn.setRequestProperty("X-Online-Host", urlparam.getHost()
+ ":" + urlparam.getPort());
urlparam = null;
} else {
conn = (HttpConnection) Connector.open(url);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* http get请求
*
* @param url
* 在基本地址http://127.0.0.1:80/+....组成
* @param type
* 固定测试地址分图片,音乐,TXT
*/
public void get(String url, int type) {
try {
conn = openConnUrl(url);
conn.setRequestMethod(HttpConnection.GET);
// 中间可能有很多属性设置
// 获得响应状态
int responseCode = conn.getResponseCode();
if (HttpConnection.HTTP_OK == responseCode) {
System.out.println("联网成功!");
InputStream is = conn.openInputStream();
displayResult(is, type);
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* http post请求
*
* @param url
* 在基本地址http://127.0.0.1:80/+....组成
* @param type
* 固定测试地址分图片,音乐,TXT
*/
public void post(String url, int type) {
try {
conn = openConnUrl(url);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
// 设置其它属性,如果没有其他属性requestProperty置为空
Hashtable requestProperty = new Hashtable();
if (requestProperty != null) {
Enumeration perpertyKeys = requestProperty.keys();
while (perpertyKeys.hasMoreElements()) {
String key = (String) perpertyKeys.nextElement();
String value = (String) requestProperty.get(key);
conn.setRequestProperty(key, value);
}
}
/**
* post正文,写一个函数组织下面请求文件,再转成byte[]; 发送post正文请求 <?xml version="1.0"
* encoding="UTF-8"?> <applyCopyrightDeclaration>
* <version></version> <appuid></appuid>
* </applyCopyrightDeclaration>
*/
Hashtable params = new Hashtable();
params.put("version", "1");
params.put("appuid", "111");
byte[] content = createCDXMLPack(true, params);
if (content != null) {
DataOutputStream dos = conn.openDataOutputStream();
dos.write(content);
dos.close();
}
// 获得响应状态
int responseCode = conn.getResponseCode();
if (HttpConnection.HTTP_OK == responseCode) {
System.out.println("联网成功!");
InputStream is = conn.openInputStream();
displayResult(is, type);
is.close();
// 成功获取数据会送到解析层
// netConnectionEvent.connSuccess(allData);
}
} catch (Exception e) {
// netConnectionEvent.connFail(e.toString());
} finally {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 生成请求版权声明文件所需要的post pack
*
* @param haveXMLHead
* 是否需要xml声明文件头
* @param params
* 键值对参数
* @return byte[] pack
*/
public static byte[] createCDXMLPack(boolean haveXMLHead, Hashtable params) {
if (params == null || params.isEmpty())
return null;
StringBuffer packBuffer = new StringBuffer();
if (haveXMLHead)
packBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
packBuffer.append("<applyCopyrightDeclaration>");
Enumeration perpertyKeys = params.keys();
while (perpertyKeys.hasMoreElements()) {
String key = (String) perpertyKeys.nextElement();
String value = (String) params.get(key);
packBuffer.append("<" + key + ">");
packBuffer.append(value);
packBuffer.append("</" + key + ">");
}
packBuffer.append("</applyCopyrightDeclaration>");
byte[] pack = null;
;
try {
pack = packBuffer.toString().getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
}
return pack;
}
/**
* 获得host,port,path便于cmwap设置
*/
class HttpURLParam {
private String _host;
private String _port;
private String _path;
public HttpURLParam(String aurl) {
int idx = aurl.indexOf("://");
if (idx == -1) {
idx = 0;
} else {
idx += 3;
}
int idx2 = aurl.indexOf('/', idx);
_host = aurl.substring(idx, idx2);
int idx3 = _host.indexOf(':');
if (idx3 == -1) {
_port = "80";
} else {
_port = _host.substring(idx3 + 1);
_host = _host.substring(0, idx3);
}
_path = aurl.substring(idx2);
}
public String getHost() {
return _host;
}
public String getPort() {
return _port;
}
public String getPath() {
return _path;
}
}
}
工程见附件: