XMLDecoder和 XMLEncoder 的樣例


  1. public static List getList(String param) {  
  2.   try {  
  3.     URL url = new URL(param.trim());  
  4.     HttpURLConnection con = (HttpURLConnection) url.openConnection();  
  5.     con.setRequestMethod("GET");  
  6.     InputStream is = con.getInputStream();  
  7.     XMLDecoder decoder = new XMLDecoder(is);  
  8.     List rtn = (List) decoder.readObject();  
  9.     is.close();  
  10.     decoder.close();  
  11.     return rtn;  
  12.   } catch (Exception ex) {  
  13.     ex.printStackTrace();  
  14.     return null;  
  15.   }  
  16. }  
  17.   
  18. public static boolean post(String param, OnlineForum of) {  
  19.   try {  
  20.     URL url = new URL(param.trim());  
  21.     HttpURLConnection con = (HttpURLConnection) url.openConnection();  
  22.     con.setRequestMethod("POST");  
  23.     con.setDoOutput(true);  
  24.     XMLEncoder encoder = new XMLEncoder(con.getOutputStream());  
  25.     encoder.writeObject(of);  
  26.     encoder.close();  
  27.     InputStream is = con.getInputStream();  
  28.     byte[] bs = new byte[1024];  
  29.     int len;  
  30.     while ((len = is.read(bs)) > 0) {  
  31.       System.out.print(new String(bs, 1, len));  
  32.     }  
  33.     System.out.println("POST OK");  
  34.     return true;  
  35.   } catch (Exception ex) {  
  36.     ex.printStackTrace();  
  37.     return false;  
  38.   }  


本文轉自

http://www.java2000.net/viewthread.jsp?tid=2497
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章