2019/06/03

C#.Net HttpClient Post / Get

這篇範例就拿RoS-BoT當對象吧,這邊所使用的瀏覽器是Chrome
首先開啟瀏覽器連到登入畫面吧,按下F12找到Network;輸入帳號密碼



按下Login按鈕之後,再Network那找到login檔案,點進去會如下圖一樣的畫面

Form data就是要Post過去的資料

Request Headers則是要傳送過去的標頭


基本上標頭我只加以下幾種,如果不確定可以透過Postman去測試
User-Agent
Origin
Referer
Content-Type
Accept-Language
Cache-Control
keep-alive

所以只要post那些data後,就可以得到cookie

接著我想要知道Bot目前狀態要怎麼取得到?跟上面步驟一樣透過瀏覽器連至Bot狀態,點擊bot-activity檔案

會看到bot-activity是Get方法取得資料,需要的標頭就那些
不過多了剛剛Post所得到的Cookie,Cookie通常用於保留資料,也可以將Cookie視為通行證
不過有的網站會需要去執行js後,才會拿到post的資料,那就直接下載js拿到.net上來做runtime也可以得知




程式碼:
/// <summary>
/// url
/// </summary>
private readonly string URL = @"https://www.ros-bot.com";
/// <summary>
/// Login url
/// </summary>
private readonly string URL_LOGIN = @"https://www.ros-bot.com/user/login";

/// <summary>
/// Post data,登入RoS-BoT網站
/// </summary>
/// <returns>result</returns>
private async Task<string> PostLoginPage(){
    var cookieContainer = new CookieContainer();
    var handler = new HttpClientHandler {
        CookieContainer = cookieContainer, 
        UseCookies = true
    };

    string result = string.Empty;
    using(var client = new HttpClient(handler)) {
        client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36");
        client.DefaultRequestHeaders.TryAddWithoutValidation("Origin", URL);
        client.DefaultRequestHeaders.TryAddWithoutValidation("Referer", URL_LOGIN);
        client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
        client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7");
        client.DefaultRequestHeaders.TryAddWithoutValidation("Cache-Control", "max-age=0");
        client.DefaultRequestHeaders.Connection.Add("keep-alive");
        client.MaxResponseContentBufferSize = int.MaxValue;
        var formUrlEncodedContent = new FormUrlEncodedContent(new [] {
            new KeyValuePair < string, string > ("form_id", "user_login"), 
            new KeyValuePair < string, string > ("op", "login"), 
            new KeyValuePair < string, string > ("name", "test@xx.com"), 
            new KeyValuePair < string, string > ("pass", "password"), 
            new KeyValuePair < string, string > ("form-build-id", "form_id")
        });

        var response = await client.PostAsync(URL_LOGIN, formUrlEncodedContent);
        var content = response.Content;
        response.EnsureSuccessStatusCode();
        this.Cookie = cookieContainer;
        result = await content.ReadAsStringAsync();
    }

    return result;
}

/// <summary>
/// 透過Get取得Bot活動Content
/// </summary>
/// <returns>html content</returns>
private async Task<string> GetBotActivity()
{
    var handler = new HttpClientHandler
    {
        CookieContainer = this.Cookie,
        UseCookies = true
    };

    string result = string.Empty;
    using (var client = new HttpClient(handler)){
        client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36");
        client.DefaultRequestHeaders.TryAddWithoutValidation("Host", URL.Replace("https://", ""));
        client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "text/html; charset=utf-8");
        client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7");
        client.DefaultRequestHeaders.TryAddWithoutValidation("Upgrade-Insecure-Requests", "1");
        client.DefaultRequestHeaders.Connection.Add("keep-alive");
        client.MaxResponseContentBufferSize = int.MaxValue;

        var response = await client.GetAsync(String.Format(URL_ACTIVITY, Id));
        var content = response.Content;
        result = await content.ReadAsStringAsync();
    }
    
    return result;
}