content: package nssa.uc.util;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.List;
  import java.util.Map;
  
  public class HttpRequest {
      /**
       * 鍚戞寚瀹歎RL鍙戦�GET鏂规硶鐨勮姹�
       * 
       * @param url
       *            鍙戦�璇锋眰鐨刄RL
       * @param param
       *            璇锋眰鍙傛暟锛岃姹傚弬鏁板簲璇ユ槸 name1=value1&name2=value2 鐨勫舰寮忋�
       * @return URL 鎵�唬琛ㄨ繙绋嬭祫婧愮殑鍝嶅簲缁撴灉
       */
      public static String sendGet(String url, String param) {
          String result = "";
          BufferedReader in = null;
          try {
              String urlNameString = url + "?" + param;
              URL realUrl = new URL(urlNameString);
              // 鎵撳紑鍜孶RL涔嬮棿鐨勮繛鎺�
              URLConnection connection = realUrl.openConnection();
              // 璁剧疆閫氱敤鐨勮姹傚睘鎬�
              connection.setRequestProperty("accept", "*/*");
              connection.setRequestProperty("connection", "Keep-Alive");
              connection.setRequestProperty("user-agent",
                      "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
              // 寤虹珛瀹為檯鐨勮繛鎺�
              connection.connect();
              // 鑾峰彇鎵�湁鍝嶅簲澶村瓧娈�
              Map<String, List<String>> map = connection.getHeaderFields();
              // 閬嶅巻鎵�湁鐨勫搷搴斿ご瀛楁
              for (String key : map.keySet()) {
                  System.out.println(key + "--->" + map.get(key));
              }
              // 瀹氫箟 BufferedReader杈撳叆娴佹潵璇诲彇URL鐨勫搷搴�
              in = new BufferedReader(new InputStreamReader(
                      connection.getInputStream()));
              String line;
              while ((line = in.readLine()) != null) {
                  result += line;
              }
          } catch (Exception e) {
              System.out.println("鍙戦�GET璇锋眰鍑虹幇寮傚父锛� + e");
              e.printStackTrace();
          }
          // 浣跨敤finally鍧楁潵鍏抽棴杈撳叆娴�
          finally {
              try {
                  if (in != null) {
                      in.close();
                  }
              } catch (Exception e2) {
                  e2.printStackTrace();
              }
          }
          return result;
      }
  
      /**
       * 鍚戞寚瀹�URL 鍙戦�POST鏂规硶鐨勮姹�
       * 
       * @param url
       *            鍙戦�璇锋眰鐨�URL
       * @param param
       *            璇锋眰鍙傛暟锛岃姹傚弬鏁板簲璇ユ槸 name1=value1&name2=value2 鐨勫舰寮忋�
       * @return 鎵�唬琛ㄨ繙绋嬭祫婧愮殑鍝嶅簲缁撴灉
       */
      public static String sendPost(String url, String param) {
          PrintWriter out = null;
          BufferedReader in = null;
          String result = "";
          try {
              URL realUrl = new URL(url);
              // 鎵撳紑鍜孶RL涔嬮棿鐨勮繛鎺�
              URLConnection conn = realUrl.openConnection();
              // 璁剧疆閫氱敤鐨勮姹傚睘鎬�
              conn.setRequestProperty("accept", "*/*");
              conn.setRequestProperty("connection", "Keep-Alive");
              conn.setRequestProperty("user-agent",
                      "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
              // 鍙戦�POST璇锋眰蹇呴』璁剧疆濡備笅涓よ
              conn.setDoOutput(true);
              conn.setDoInput(true);
              // 鑾峰彇URLConnection瀵硅薄瀵瑰簲鐨勮緭鍑烘祦
              out = new PrintWriter(conn.getOutputStream());
              // 鍙戦�璇锋眰鍙傛暟
              out.print(param);
              // flush杈撳嚭娴佺殑缂撳啿
              out.flush();
              // 瀹氫箟BufferedReader杈撳叆娴佹潵璇诲彇URL鐨勫搷搴�
              in = new BufferedReader(
                      new InputStreamReader(conn.getInputStream()));
              String line;
              while ((line = in.readLine()) != null) {
                  result += line;
              }
          } catch (Exception e) {
              System.out.println("鍙戦� POST 璇锋眰鍑虹幇寮傚父锛�+e");
              e.printStackTrace();
          }
          //浣跨敤finally鍧楁潵鍏抽棴杈撳嚭娴併�杈撳叆娴�
          finally{
              try{
                  if(out!=null){
                      out.close();
                  }
                  if(in!=null){
                      in.close();
                  }
              }
              catch(IOException ex){
                  ex.printStackTrace();
              }
          }
          return result;
      }    
  }