public String requestGET(String URLString,String URL) throws IOException{
// =============================================================
// URLString是HTTP地址,URL为后面的参数
// 这里的例子是发送用的用户名和密码到服务器端进行用户验证
// 比如 String URLString = "http://192.168.0.1:8080/login.jsp"
// String URL = "?Name="+this.txtName+"&Pass="+this.txtPass
// =============================================================
HttpConnection hpc = null;
DataInputStream dis = null;
boolean newline = false;
String content = "";
try{
// ===========================================================
// 建立连接
// ===========================================================
hpc = (HttpConnection)Connector.open(URLString+URL);
hpc.setRequestMethod(HttpConnection.GET);
dis = new DataInputStream(hpc.openInputStream());
int character;
// ===========================================================
// 读取返回的HTTP内容
// ===========================================================
while((character = dis.read()) != -1){
if((char)character == '//'){
newline = true;
continue;
}
else{
if((char)character =='n'&& newline){
content +="/n";
newline = false;
}
else if(newline){
content +="//" +(char)character;
newline = false;
}
else{
content +=(char)character;
newline = false;
}
}
}
}
catch(IOException e){
System.out.PRint("ERROR:"+e);
}
finally{
if(hpc != null){
hpc.close();
hpc = null;
}
if(dis != null){
dis.close();
}
}
// ===============================================================
// 由于内容可能有中文,所以在接受到信息后要对内容进行字符集的转换
// ===============================================================
content = (unicodeTogb2312(content)).trim();
return content;
}
public static String unicodeTogb2312(String s){
if (s==null){ return ""; }
if (s.equals("")){ return s; }
try{
return new String(s.getBytes("ISO8859_1"),"gb2312");
}
catch(Exception uee){
return s;
}
}
public boolean SocketConn(String s) throws IOException{
// =============================================================
// s是Socket连接字符串
// 这里的例子是发送用的用户名和密码到服务器端进行用户验证
// 比如 String s = "socket://192.168.0.1:6666"
// =============================================================
private StreamConnection conServer;
private String strServerAddr;
private boolean bConnected;
conServer = null;
strServerAddr = s; // 连接地址
bConnected = false; // 连接状态
try
{
conServer = (StreamConnection)Connector.open(strServerAddr);
}
catch(Exception exception)
{
System.out.println("Connect server error");
bConnected = false;
return false;
}
bConnected = true;
System.out.println("connect ok!");
return true;
}
..........
try{
// 建立端口为6666的socket服务器
ServerSocketConnection SocketSer;
SocketSer = (ServerSocketConnection)Connector.open("socket://:6666");
// 等待客户端连接
SocketConnection sc;
// 如有连接,则新增一个线程对连接进行处理
sc = (SocketConnection)SocketSer.acceptAndOpen();
..........
while(true){
// 对sc的InputStream和OutPutStream进行处理
}
}
..........
protected boolean sendData(byte abyte0[])//自己替换[]
{
System.out.println("send :" + bConnected);
// 判断连接情况
if(!bConnected)
return false;
OutputStream outputstream = null;
try
{
outputstream = conServer.openOutputStream();
// 写信息到outputstream中
outputstream.write(abyte0);
// 我的理解是强制送出所有已经写了的信息
outputstream.flush();
outputstream.close();
}
catch(Exception exception)
{
System.out.println("Send Data error");
bConnected = false;
try
{
if(outputstream != null)
outputstream.close();
// 调用断开连接的函数
disconnect();
}
catch(Exception exception1) { }
return false;
}
return true;
}
(出处:http://www.VeVb.com)
新闻热点
疑难解答