- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpException;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.NameValuePair;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.PostMethod;
- public class WebHttpClient {
- /**
- * 獲得網頁中的所有HTML內容
- * @param url
- * @param charset
- * @return
- */
- public String getWebContentByGet(String url,String charset){
- HttpClient client = new HttpClient();
- GetMethod getMethod = new GetMethod(url);
- StringBuilder sb = new StringBuilder();
- try {
- // 狀態碼
- int statusCode=client.executeMethod(getMethod);
- if (statusCode == HttpStatus.SC_OK) {
- //獲得HTML文本
- BufferedReader bf = new BufferedReader(new InputStreamReader(
- getMethod.getResponseBodyAsStream(), charset));
- String line = null;
- while ((line = bf.readLine()) != null) {
- sb.append(line).append("/r/n");
- }
- bf.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- getMethod.releaseConnection();
- }
- return sb.toString();
- }
- /**
- * 獲得網頁中的所有HTML內容
- * @param url
- * @param mapData:傳遞的參數
- * @param charset
- * @return
- */
- public String getWebContentByPost(String url,Map<String,String> mapData,String charset){
- HttpClient client = new HttpClient();
- PostMethod postMethod = new PostMethod(url);
- StringBuilder sb = new StringBuilder();
- // 填入各個表單域的值
- NameValuePair[] data = new NameValuePair[mapData.size()];
- Set set = mapData.entrySet();
- Iterator iterator = set.iterator();
- int i=0;
- while (iterator.hasNext()) {
- Map.Entry entry = (Map.Entry) iterator.next();
- data[i]=new NameValuePair((String)entry.getKey(),(String)entry.getValue());
- i++;
- }
- /*
- NameValuePair[] data = {new NameValuePair("toPath","toPath"),
- new NameValuePair("action","login"),
- new NameValuePair("loginname","13761083826"),
- new NameValuePair("password","111111")
- };
- */
- // 將表單的值放入postMethod中
- postMethod.setRequestBody(data);
- try {
- int statusCode = client.executeMethod(postMethod);
- if (statusCode == HttpStatus.SC_OK) {
- //獲得HTML文本
- BufferedReader bf = new BufferedReader(new InputStreamReader(
- postMethod.getResponseBodyAsStream(), charset));
- String line = null;
- while ((line = bf.readLine()) != null) {
- sb.append(line).append("/r/n");
- }
- bf.close();
- }
- } catch (HttpException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally {
- postMethod.releaseConnection();
- }
- return sb.toString();
- }
- public static void main(String[] str) {
- //get
- WebHttpClient util=new WebHttpClient();
- String content=util.getWebContentByGet("http://www.baidu.com", "gb2312");
- System.out.println(content);
- //post
- // Map<String,String> map=new HashMap<String,String>();
- // map.put("toPath","toPath");
- // map.put("action","login");
- // map.put("loginname","13761083826");
- // map.put("password","111111");
- // String content=util.getWebContentByPost("http://localhost:8080/Lottery/login.portal",map, "UTF-8");
- // System.out.println(content);
- }
- }
- http://apache.freelamp.com/httpcomponents/commons-httpclient/binary/commons-httpclient-3.1.zip
J2EE工具類:WebHttpClient.java
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.