- public static List getList(String param) {
- try {
- URL url = new URL(param.trim());
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("GET");
- InputStream is = con.getInputStream();
- XMLDecoder decoder = new XMLDecoder(is);
- List rtn = (List) decoder.readObject();
- is.close();
- decoder.close();
- return rtn;
- } catch (Exception ex) {
- ex.printStackTrace();
- return null;
- }
- }
- public static boolean post(String param, OnlineForum of) {
- try {
- URL url = new URL(param.trim());
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("POST");
- con.setDoOutput(true);
- XMLEncoder encoder = new XMLEncoder(con.getOutputStream());
- encoder.writeObject(of);
- encoder.close();
- InputStream is = con.getInputStream();
- byte[] bs = new byte[1024];
- int len;
- while ((len = is.read(bs)) > 0) {
- System.out.print(new String(bs, 1, len));
- }
- System.out.println("POST OK");
- return true;
- } catch (Exception ex) {
- ex.printStackTrace();
- return false;
- }
- }
本文轉自
http://www.java2000.net/viewthread.jsp?tid=2497