java中模擬提交表單登錄等

post提交表單

導入httpclient包

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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;
import org.apache.http.NameValuePair;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public void postForm() {
        // 創建默認的httpClient實例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 創建httppost
        HttpPost httppost = new HttpPost("http://vod.syau.edu.cn");
        // 創建參數隊列
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("username", "admin"));
        formparams.add(new BasicNameValuePair("password", "123456"));
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    System.out.println("--------------------------------------");
                    System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
                    System.out.println("--------------------------------------");
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 關閉連接,釋放資源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

get請求頁面HTML

	//get請求獲得頁面
    public String getHtml(String url){
        CloseableHttpClient httpClient = ReturnObject.httpClient;
        HttpGet httpGet = new HttpGet(url);

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpEntity entity = response.getEntity();
        try {
            return EntityUtils.toString(entity,"utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

模擬瀏覽器發送get請求登錄

public void method(){
	//1.生成httpclient,相當於該打開一個瀏覽器
    CloseableHttpClient httpClient1 = httpclient;
    CloseableHttpResponse response1 = null;
    //2.創建get請求,相當於在瀏覽器地址欄輸入 網址
    HttpGet request = new HttpGet("http://210.47.163.27:9002/gradeLnAllAction.do?type=ln&oper=lnfaqk&flag=zx");
    try {
       //3.執行get請求,相當於在輸入地址欄後敲回車鍵
        response1 = httpClient1.execute(request);

        //4.判斷響應狀態爲200,進行處理
        if(response1.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        //5.獲取響應內容
        HttpEntity httpEntity = response1.getEntity();
        String html = EntityUtils.toString(httpEntity, "GBK");
        System.out.println(html);
        } else {
        //如果返回狀態不是200,比如404(頁面不存在)等,根據情況做處理,這裏略
        System.out.println("返回狀態不是200");
        System.out.println(EntityUtils.toString(response1.getEntity(), "utf-8"));
                    }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
             e.printStackTrace();
         } finally {
        //6.關閉
         HttpClientUtils.closeQuietly(response1);
          HttpClientUtils.closeQuietly(httpClient1);
       }
}
發佈了52 篇原創文章 · 獲贊 10 · 訪問量 9195
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章