HttpÇëÇó²âÊÔÀà(ËÄ)

2014-11-24 09:07:05 ¡¤ ×÷Õß: ¡¤ ä¯ÀÀ: 2
y("line.separator"); while (tempLine != null) { tempStr.append(tempLine); tempStr.append(crlf); tempLine = rd.readLine(); } responseContent = tempStr.toString(); rd.close(); in.close(); } catch (IOException e) { System.err.println("ÍøÂç¹ÊÕÏ"); e.printStackTrace(); } finally { if (url_con != null) { url_con.disconnect(); } } return responseContent; } public static String doPost(String reqUrl, Map parameters) { HttpURLConnection url_con = 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(), HttpRequestProxy.requestEncoding)); params.append("&"); } if (params.length() > 0) { params = params.deleteCharAt(params.length() - 1); } URL url = new URL(reqUrl); 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(getConnectTimeOut()); // £¨µ¥Î»£ººÁÃ룩jdk1.5»»³ÉÕâ¸ö,Á¬½Ó³¬Ê± url_con.setReadTimeout(getConnectTimeOut()); // £¨µ¥Î»£ººÁÃ
룩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, getRequestEncoding())); 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) { System.err.println("ÍøÂç¹ÊÕÏ"); e.printStackTrace(); } finally { if (url_con != null) { url_con.disconnect(); } } return responseContent; } /** * @return Á¬½Ó³¬Ê±(ºÁÃë) * @see com.hengpeng.common.web.HttpRequestProxy#connectTimeOut */ public static int getConnectTimeOut() { return HttpRequestProxy.connectTimeOut; } /** * @return ¶ÁÈ¡Êý¾Ý³¬Ê±(ºÁÃë) * @see com.hengpeng.common.web.HttpRequestProxy#readTimeOut */ public static int getReadTimeOut() { return HttpRequestProxy.readTimeOut; } /** * @return ÇëÇó±àÂë * @see com.hengpeng.common.web.HttpRequestProxy#requestEncoding */ public static String getRequestEncoding() { return requestEncoding; } /** * @param connectTimeOut * Á¬½Ó³¬Ê±(ºÁÃë) * @see com.hengpeng.common.web.HttpRequestProxy#connectTimeOut */ publ