socket 發送帶參數的http

public class Demo {

  public static void main(String[] args) throws Exception {
    Map m = new HashMap();
    String url = "http://www.ecice06.com/cn/search_gjdo.asp";
    String code = "GB2312";

//    m.put("sel_zazhimc", "");
//
//    m.put("sel_niandu", "");
//
//    m.put("txt_qishiye", "");
//
//    m.put("txt_doi", "");
//    m.put("xueke", "");
//    m.put("zhuanye", "");
//    m.put("txt_zuozhe", "");
//    m.put("txt_zuozhe2", "");
//    m.put("txt_zuozhedw", "");
//
//    m.put("txt_zhaiyao", "");
//    m.put("txt_guanjianci", "");
//    m.put("txt_fenleihao", "");
//    m.put("sel_niandus", "");
//    m.put("sel_niandue", "");

    m.put("txt_wenti", "數據");
    m.put("pagesize", "10");
    m.put("Submit2", "查詢");
    m.put("rad_px", "zuozhexm,kanchurq desc");
    String rus = doPost(url, m, code);

    System.out.println(rus);
  }

  public static String doPost(String reqUrl, Map parameters, String recvEncoding) {
    HttpURLConnection conn = null;
    String responseContent = null;
    try {
      StringBuffer params = new StringBuffer();
      for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) {
        Entry element = (Entry) iter.next();
        params.append(element.getKey().toString());
        params.append("=");
        params.append(URLEncoder.encode(element.getValue().toString(), recvEncoding));
        params.append("&");
      }

      if (params.length() > 0) {
        params = params.deleteCharAt(params.length() - 1);
      }
      URL url = new URL(reqUrl);
      HttpURLConnection url_con = (HttpURLConnection) url.openConnection();
      url_con.setRequestMethod("POST");
      // System.setProperty("sun.net.client.defaultConnectTimeout", String
      // .valueOf(HttpRequestProxy.connectTimeOut));// (單位:毫秒)jdk1.4換成這個,連接超時
      // System.setProperty("sun.net.client.defaultReadTimeout", String
      // .valueOf(HttpRequestProxy.readTimeOut)); // (單位:毫秒)jdk1.4換成這個,讀操作超時
      url_con.setConnectTimeout(5000);//(單位:毫秒)jdk
      // 1.5換成這個,連接超時
      url_con.setReadTimeout(5000);//(單位:毫秒)jdk 1.5換成這個,讀操作超時
      url_con.setDoOutput(true);
      byte[] b = params.toString().getBytes();
      url_con.getOutputStream().write(b, 0, b.length);
      url_con.getOutputStream().flush();
      url_con.getOutputStream().close();

      InputStream in = url_con.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(in, recvEncoding));
      String tempLine = rd.readLine();
      StringBuffer tempStr = new StringBuffer();
      String crlf = System.getProperty("line.separator");
      while (tempLine != null) {
        tempStr.append(tempLine);
        tempStr.append(crlf);
        tempLine = rd.readLine();
      }
      responseContent = tempStr.toString();
      rd.close();
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (conn != null) {
        conn.disconnect();
      }
    }
    return responseContent;
  }

}

發佈了16 篇原創文章 · 獲贊 8 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章