httpcilent实例

httpclient主要用到get与post方式:
get:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://........");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
System.out.println(entity.getContentType().toString());
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
String s = "";
while((s=br.readLine())!=null){
System.out.println(s.toString());
}

post:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.renren.com/pLogin.do");
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("email",""));
nvps.add(new BasicNameValuePair("password",""));
httppost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println(entity.getContentType().toString());
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(),"utf-8"));
String s = "";
while((s=br.readLine())!=null){
System.out.println(s.toString());
}

你可能感兴趣的:(网络爬虫)