我們可以使用下面代碼讓 HttpClient 使用 Cookie ,有了這個才可以保存登陸,不然登陸成功下次訪問網頁還是沒登陸。
CookieContainer cookies = new CookieContainer(); HttpClientHandlerhandler= new HttpClientHandler();handler.CookieContainer = cookies;HttpClient http = new HttpClient(handler);
雖然已經有Cookie,但是還缺少一些請求需要帶的頭,因為瀏覽器是會告訴網站,需要的Accept,為了假裝這是一個瀏覽器,所以就需要添加Accept 和Accept-Encoding Accept-Language User-Agent
添加 Accept
下面的代碼可以添加Accept,這里后面的字符串可以自己使用瀏覽器查看,復制。
http.DefaultRequestHeaders.TryAddWithoutValidation("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");添加 Accept-Encoding http.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding","gzip, deflate, br");
如果有 gzip 就需要解壓,這個現在不太好弄,建議不要加。
添加 Accept-Language http.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language","zh-CN,zh;q=0.8");添加 User-Agent http.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
ContentType
如果設置 ContentType 需要在發送的內容進行添加
content = new StringContent("{"loginName":"lindexi","password":"csdn","autoLogin":false}"){Headers ={ContentType = new MediaTypeHeaderValue("application/json")}}; 發送數據
如果需要使用 Post 或 get 發送數據,那么可以使用HttpContent做出數據,提供的類型有StringContent、FormUrlEncodedContent等。
其中StringContent最簡單,而FormUrlEncodedContent可以自動轉換。
str= $"username={account.UserName}&password={account.Key}<={lt}&execution={execution}&_eventId=submit";str=str.Replace("@","%40"); HttpContent content =newStringContent(str, Encoding.UTF8);
上面代碼就是使用 StringContent 可以看到需要自己轉換特殊字符,當然一個好的方法是使用 urlencoding 轉換。
如果使用FormUrlEncodedContent就不需要做轉換
content=newFormUrlEncodedContent(newList
如果需要上傳文件,那么需要使用MultipartFormDataContent
content = new MultipartFormDataContent();((MultipartFormDataContent)content).Headers.Add("name","file1");((MultipartFormDataContent)content).Headers.Add("filename","20170114120751.png");var stream = new StreamContent(await File.OpenStreamForReadAsync());((MultipartFormDataContent)content).Add(stream);登陸方法
打開 https://passport.csdn.net/account/login 可以看到這個界面
右擊查看源代碼,可以拿到上傳需要使用的兩個變量 lt 和 execution
?
在登陸的時候,使用 post 把賬號密碼、lt execution 上傳就可以登陸
模擬登陸csdn
于是下面就是模擬登陸
獲得賬號信息
AccountCimage account = AppId.AccoutCimage;
cookie
CookieContainer cookies = new CookieContainer(); HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookies; HttpClient http = new HttpClient(handler);
獲得登陸需要的流水號
var url = new Uri("https://passport.csdn.net/account/login"); http.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); //http.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, br"); http.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "zh-CN,zh;q=0.8"); http.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"); handler.UseCookies = true; handler.AllowAutoRedirect = true; string str = await http.GetStringAsync(url); Regex regex = new Regex(" type="hidden" name="lt" value="([\w|\-]+)""); var lt = regex.Match(str).Groups[1].Value; regex = new Regex("type="hidden" name="execution" value="(\w+)""); var execution = regex.Match(str).Groups[1].Value;
登陸
str = $"username={account.UserName}&password={account.Key}<={lt}&execution={execution}&_eventId=submit"; str = str.Replace("@", "%40"); HttpContent content = new StringContent(str, Encoding.UTF8); str = await content.ReadAsStringAsync(); content=new FormUrlEncodedContent(new List
查看登陸
url = new Uri(““);
str = await http.GetStringAsync(url);
上傳文件
content = new MultipartFormDataContent(); ((MultipartFormDataContent)content).Headers.Add("name", "file1"); ((MultipartFormDataContent)content).Headers.Add("filename", "20170114120751.png"); var stream = new StreamContent(await File.OpenStreamForReadAsync()); ((MultipartFormDataContent)content).Add(stream); str = await ((MultipartFormDataContent)content).ReadAsStringAsync(); url = new Uri("http://write.blog.csdn.net/article/UploadImgMarkdown?parenthost=write.blog.csdn.net"); var message = await http.PostAsync(url, content); if (message.StatusCode == HttpStatusCode.OK) { ResponseImage(message); } private async void ResponseImage(HttpResponseMessage message) { using (MemoryStream memoryStream = new MemoryStream()) { int length = 1024; byte[] buffer = new byte[length]; using (GZipStream zip = new GZipStream(await message.Content.ReadAsStreamAsync(), CompressionLevel.Optimal)) { int n; while ((n = zip.Read(buffer, 0, length)) > 0) { memoryStream.Write(buffer, 0, n); } } using (StreamReader stream = new StreamReader(memoryStream)) { string str = stream.ReadToEnd(); } } }使用 WebView 模擬登陸 csdn
下面給大家一個叫簡單方法模擬登陸csdn
GeekWebView.Navigate(newUri("http://passport.csdn.net/")); GeekWebView.NavigationCompleted += OnNavigationCompleted; F =async() => {varfunctionString =string.Format(@"document.getElementsByName('username')[0].value='{0}';","lindexi_gd@163.com");awaitGeekWebView.InvokeScriptAsync("eval",newstring[] { functionString }); functionString =string.Format(@"document.getElementsByName('password')[0].value='{0}';","密碼");awaitGeekWebView.InvokeScriptAsync("eval",newstring[] { functionString }); functionString =string.Format(@"document.getElementsByClassName('logging')[0].click();");awaitGeekWebView.InvokeScriptAsync("eval",newstring[] { functionString }); };privateAction F {set;get; }privatevoidOnNavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args) { F(); }當然,這時需要修改登陸信息,我上面寫的是我的。如果遇到有驗證碼,那么這個方法是不可使用,因為輸入驗證碼暫時還沒法做。
評論
查看更多