三個簡單的HTTPClient4.3小例子html
寫三個httpclient的小例子,但願對剛開始學習httpclient的小夥伴們有一個參考↖(^ω^)↗java
一、簡單的獲取一個HTML頁面的內容 apache
public void getHTML() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("http://www.baidu.com/"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); String html = EntityUtils.toString(entity); httpclient.close(); }
二、下載一個圖片到本地post
public class DownloadFile { public static void main(String[] args) throws Exception { //圖片的網址 String url = "http://ww2.sinaimg.cn/large/9d57a855jw1dqpv9fp4yuj.jpg"; String destfilename = "D:\\TDDOWNLOAD\\cat.jpg"; //生成一個httpclient對象 CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); File file = new File(destfilename); try { FileOutputStream fout = new FileOutputStream(file); int l = -1; byte[] tmp = new byte[1024]; while ((l = in.read(tmp)) != -1) { fout.write(tmp,0,l); //注意這裏若是用OutputStream.write(buff)的話,圖片會失真,你們能夠試試 } fout.flush(); fout.close(); } finally { // 關閉低層流。 in.close(); } httpclient.close(); } }
3.post模擬人人網登錄 學習
import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class LoginRR { private CloseableHttpClient httpclient; private HttpPost httppost;//用於提交登錄數據 private HttpGet httpget;//用於得到登陸後的頁面 private String login_success;//用於構造上面的HttpGet public LoginRR() { httpclient = HttpClients.createDefault(); //人人的登錄界面網址 httppost = new HttpPost("http://www.renren.com/PLogin.do"); } public void logIn(String name, String password) throws Exception { //打包將要傳入的參數 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("email", name)); params.add(new BasicNameValuePair("password",password)); httppost.setEntity(new UrlEncodedFormEntity(params)); try { //提交登陸數據 HttpResponse re = httpclient.execute(httppost); //得到跳轉的網址 Header locationHeader = re.getFirstHeader("Location"); //登錄不成功 if (locationHeader == null) { System.out.println("登錄不成功,請稍後再試!"); return; } else//成功 { login_success=locationHeader.getValue();//獲取登錄成功以後跳轉連接 System.out.println("成功以後跳轉到的網頁網址:"+login_success); } }catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void PrintText() throws IOException { httpget = new HttpGet(login_success); HttpResponse re2 = null; try { re2 = httpclient.execute(httpget); //輸出登陸成功後的頁面 String str=EntityUtils.toString(re2.getEntity()); System.out.println(str); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { httppost.abort(); httpget.abort(); httpclient.close(); } } public static void main(String[] args) throws Exception { String name = "username", password = "password "; //本身的帳號,口令 LoginRR lr = new LoginRR(); lr.logIn(name, password); lr.PrintText(); } }