j2me http联网客户端的编写之一
2006-08-17 10:49
309 查看
建立与服务器的连接
1.采用post方式进行数据传送
顾名思义,post 方式就是用来向服务器发送数据
HttpConnection hc = null;
InputStream is = null;
String agent = "Profile/MIDP-1.0 Configuration/CLDC-1.0";
String type = "application/x-www-form-urlencoded";
byte result[] = null;
try {
hc = (HttpConnection) Connector.open(url);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("User-Agent", agent);
hc.setRequestProperty("Content-Type", type);
hc.setRequestProperty("Content-Length", new Integer(rawData.length()).toString());//必须有这个,否则发送不成功
OutputStream os = hc.openOutputStream();
os.write(rawData.getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
is = hc.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
baos.write(ch);
}
result = baos.toByteArray();
baos.close();
} catch (Exception e) {
e.printStackTrace();
result = null;
throw e;
} finally {
try {
if (is != null) {
is.close();
is = null;
}
if (hc != null) {
hc.close();
hc = null;
}
} catch (Exception e) {
e.printStackTrace();
result = null;
throw e;
}
}
return result;
2.采用get方式进行数据传送
顾名思义,get方式就是向服务器读取数据
HttpConnection hc = null;
InputStream is = null;
String agent = "Profile/MIDP-1.0 Configuration/CLDC-1.0";
String type = "application/x-www-form-urlencoded";
byte result[] = null;
try {
hc = (HttpConnection) Connector.open(url + "?" + rawData);//把请求附加到url后面
hc.setRequestMethod(HttpConnection.GET);
hc.setRequestProperty("User-Agent", agent);
hc.setRequestProperty("Content-Language", "en-CA");
hc.setRequestProperty("Content-Type", type);
hc.setRequestProperty("Connection", "Keep-Alive");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
is = hc.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
baos.write(ch);
}
result = baos.toByteArray();
baos.close();
} catch (Exception e) {
e.printStackTrace();
result = null;
throw e;
} finally {
try {
if (is != null) {
is.close();
is = null;
}
if (hc != null) {
hc.close();
hc = null;
}
} catch (Exception e) {
e.printStackTrace();
result = null;
throw e;
}
}
return result;
}
1.采用post方式进行数据传送
顾名思义,post 方式就是用来向服务器发送数据
HttpConnection hc = null;
InputStream is = null;
String agent = "Profile/MIDP-1.0 Configuration/CLDC-1.0";
String type = "application/x-www-form-urlencoded";
byte result[] = null;
try {
hc = (HttpConnection) Connector.open(url);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("User-Agent", agent);
hc.setRequestProperty("Content-Type", type);
hc.setRequestProperty("Content-Length", new Integer(rawData.length()).toString());//必须有这个,否则发送不成功
OutputStream os = hc.openOutputStream();
os.write(rawData.getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
is = hc.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
baos.write(ch);
}
result = baos.toByteArray();
baos.close();
} catch (Exception e) {
e.printStackTrace();
result = null;
throw e;
} finally {
try {
if (is != null) {
is.close();
is = null;
}
if (hc != null) {
hc.close();
hc = null;
}
} catch (Exception e) {
e.printStackTrace();
result = null;
throw e;
}
}
return result;
2.采用get方式进行数据传送
顾名思义,get方式就是向服务器读取数据
HttpConnection hc = null;
InputStream is = null;
String agent = "Profile/MIDP-1.0 Configuration/CLDC-1.0";
String type = "application/x-www-form-urlencoded";
byte result[] = null;
try {
hc = (HttpConnection) Connector.open(url + "?" + rawData);//把请求附加到url后面
hc.setRequestMethod(HttpConnection.GET);
hc.setRequestProperty("User-Agent", agent);
hc.setRequestProperty("Content-Language", "en-CA");
hc.setRequestProperty("Content-Type", type);
hc.setRequestProperty("Connection", "Keep-Alive");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
is = hc.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
baos.write(ch);
}
result = baos.toByteArray();
baos.close();
} catch (Exception e) {
e.printStackTrace();
result = null;
throw e;
} finally {
try {
if (is != null) {
is.close();
is = null;
}
if (hc != null) {
hc.close();
hc = null;
}
} catch (Exception e) {
e.printStackTrace();
result = null;
throw e;
}
}
return result;
}
相关文章推荐
- j2me http联网客户端编写之二
- Unity3d搭建HTTP弱联网的服务器搭建及客户端编写(二)之php服务器
- [置顶] Unity3d搭建HTTP弱联网的服务器搭建及客户端编写(一)之java服务器
- 【转载】Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据,作者:Jaiky_杰哥
- 在J2ME联网应用中获得客户端的手机号码
- Unity 多人联网客户端编写
- j2me,Http,l联网
- 在J2ME联网应用中获得客户端的手机号码
- Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据
- J2ME连接HTTP,获取网页信息的联网类
- 用I/O完成端口+socket编写客户端demo实现Http下载
- 在J2ME联网应用中获得客户端的手机号码
- 在J2ME联网应用中获得客户端的手机号码
- 使用libcurl 编写Linux下Http客户端
- 使用libcurl库编写HTTP客户端(包括GET/POST/HTTPS)
- 使用Poco库编写一个简单的http客户端原理
- j2me Socket编程常用服务器和客户端编写结构
- 开发J2ME低级联网应用
- Matlab 编写 COM 客户端
- pica客户端J2ME源码,100%可运行可修改