C#使用WebClient類來模擬登錄表單

wufflina 8年前發布 | 862 次閱讀 C# c# Webclient
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WebClient wc = new WebClient())
            {
                NameValueCollection nvc = new NameValueCollection();                

                nvc.Add("UserName", "xxx");
                nvc.Add("PassWord", "495e5");
                nvc.Add("Btn_Login.x", "19");
                nvc.Add("Btn_Login.y", "0");
                nvc.Add("challenge", "ddd");
                nvc.Add("type", "1");
                //添加必要的http請求頭
                wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4 AlexaToolbar/alxg-3.1");
                wc.Headers.Add(HttpRequestHeader.Referer, "http://outofmemory.cn/");
                wc.Headers.Add("Origin", "http://outofmemory.cn");
                var bytes = wc.UploadValues("http://outofmemory.cn/", nvc);
                string responseHtml = Encoding.UTF8.GetString(bytes);

                //看responseHtml是否是登錄后的結果
                String cookie = wc.ResponseHeaders[HttpResponseHeader.SetCookie];
                //如果登錄成功可以使用此cookie繼續執行其他操作

            }
        }
    }
}
 本文由用戶 wufflina 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!