大家都知道HttpClient可以抓取頁面數據,但是有的頁面需要用戶登錄后才可以訪問,第一次我用瀏覽器登錄了,把瀏覽器的Cookie放了進去,可以抓取,但是一天后服務器的session就過期了,這樣很麻煩,后來在網上找了很多資料,才有了下面的版本,下面需要設置兩個URL,一個是登錄頁的,主要是用來獲取登錄后的Cookie,然后就可以請求第二次的URL了。代碼很簡單應該大家都可以看懂,我就不解釋了。
package cn.amazon.http;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.http.HttpEntity;import org.apache.http.HttPResponse;import org.apache.http.NameValuePair;import org.apache.http.client.CookieStore;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.impl.conn.PoolingClientConnectionManager;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;//對接口進行測試 public class getCookie { private String loginUrl = ""; private String SearchUrl = ""; private String charset = "UTF-8"; public void test() { //存放發送參數 Map<String, String> createMap = new HashMap<String, String>(); createMap.put("userName", ""); createMap.put("passWord", ""); createMap.put("email", "huayanh@sellercs.amazon.com"); HttpPost httpPost = null; HttpPost httpPost2 = null; HttpResponse response = null; DefaultHttpClient client = null; String result = null; try { client = new DefaultHttpClient(new PoolingClientConnectionManager()); httpPost = new HttpPost(loginUrl); // 設置請求頭 httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"); // 設置參數 List<NameValuePair> list = new ArrayList<NameValuePair>(); Iterator iterator = createMap.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, String> elem = (Entry<String, String>) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(), elem.getValue())); } if (list.size() > 0) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset); httpPost.setEntity(entity); } // 第一次請求 response = client.execute(httpPost); System.out.println(response); // 第二次請求 httpPost2 = new HttpPost(SearchUrl); response = client.execute(httpPost2); System.out.println(response); // 登錄后的請求內容 if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } // System.out.println(result); } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String[] args) { getCookie main = new getCookie(); main.test(); }}新聞熱點
疑難解答