Ifree团队 2012-05-23
/** * 访问网络 * @param path * @param map * @return */ public static String post(String path,Map<String, Object> map){ DefaultHttpClient httpClient =new DefaultHttpClient(); String returnStr = ""; HttpPost httpPost = new HttpPost(path); HttpResponse resPonse = null; List<NameValuePair> params = new ArrayList<NameValuePair>(); if(map != null){ Set<String> keys = map.keySet(); Iterator<String> iter = keys.iterator(); while(iter.hasNext()){ String key = iter.next(); params.add(new BasicNameValuePair(key, map.get(key).toString())); } try { httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setStaleCheckingEnabled(httpParams, false); HttpConnectionParams.setConnectionTimeout(httpParams, 5000); HttpConnectionParams.setSoTimeout(httpParams, 5000); httpClient.setParams(httpParams); httpClient.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); resPonse = httpClient.execute(httpPost); if(resPonse.getStatusLine().getStatusCode() == 200){ returnStr = converString(resPonse.getEntity().getContent()); } if(returnStr != null && returnStr.equals("")){ returnStr = returnStr.replaceAll("\\\\\"", "\\\"").substring(1); returnStr = returnStr.substring(0, returnStr.length()-1); } } catch (Exception e) { }finally{ httpPost.abort(); } } return returnStr; } /** * 解析返回流 * @param is * @return */ public static String converString(InputStream is){ StringBuffer sb = new StringBuffer(); try { BufferedReader br = new BufferedReader(new InputStreamReader(is), 512*1024); String line = ""; while((line = br.readLine()) != null){ sb.append(line); } } catch (Exception e) { // TODO: handle exception } return sb.toString(); }