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 +="http://" +(char)character;
newline = false;
}
else{
content +=(char)character;
newline = false;
}
}
}
}
catch(IOException e){
System.out. }
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.companysz.com)
新聞熱點
疑難解答