使用HttpClient获取网页源码
需要的包:commons-codec-1.3.jar commons-httpclient-3.1.jar
/** * 通过get方式取得html * @param url * @param timeout * @param charset * @return */ public static String getHTMLByUrl(String url, int timeout, String charset){ if (charset == null) charset = "utf-8"; HttpMethod result = new GetMethod(url); return doHttp(result,timeout,charset); } /** * 通过post方式取得html * @param url * @param timeout * @param charset * @param params * @return */ public static String getHTMLByUrl(String url, int timeout, String charset,Map<String,String> params){ if (charset == null) charset = "utf-8"; PostMethod result = new PostMethod(url); NameValuePair[] nameValues =new NameValuePair[params.size()]; NameValuePair simcard; String[] keys=params.keySet().toArray(new String[params.size()]); String key; for(int i=0;i<keys.length;i++){ key=keys[i]; simcard = new NameValuePair(key,params.get(key)); nameValues[i]=simcard; } result.setRequestBody(nameValues); return doHttp(result,timeout,charset); } public static String doHttp(HttpMethod result,int timeout, String charset){ HttpClient client = new HttpClient(); try { HttpConnectionManagerParams managerParams = client .getHttpConnectionManager().getParams(); managerParams.setConnectionTimeout(timeout); client.executeMethod(result); InputStream resStream = result.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(resStream, charset)); StringBuffer resBuffer = new StringBuffer(); String resTemp = ""; while ((resTemp = br.readLine()) != null) { resBuffer.append(resTemp); } return resBuffer.toString(); } catch (HttpException e) { return null; } catch (IOException e) { return null; } catch (Exception e) { return null; } finally { result.releaseConnection(); } }