当前位置: 移动技术网 > IT编程>开发语言>.net > C# GET/POST 发送HTTP请求,自动提交表单,模拟网页登录/网页注册。填坑

C# GET/POST 发送HTTP请求,自动提交表单,模拟网页登录/网页注册。填坑

2020年07月22日  | 移动技术网IT编程  | 我要评论

经历了多天的debug,苦苦挣扎,终于完成了。网上找答案是真难找到,全是多少年前的回答了,都不使用了现在。

算了,我码码字好了,文笔不好见谅!不懂的可以问我,看到会回!

首先,上代码:

                static readonly HttpClient client = new HttpClient();

                CookieContainer cookie = new CookieContainer();

                client.DefaultRequestHeaders.Add("Cookie", "__jsluid_h=f7da56c473b5f44fe10d2272ca09bb70;"
                    + "UM_distinctid=1734b254b23199-0caffe46dc6546-4353760-13c680-1734b254b241c7;"
                    + "ASP.NET_SessionId=0vyycsk3nnprcdjst5kziguc;"
                    + "CNZZDATA1744450=cnzz_eid%3D1711498581-1594689246-%26ntime%3D1595299278");
                
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");
                client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
                client.DefaultRequestHeaders.Add("Connection", "keep-alive");
                HttpResponseMessage response = await client.PostAsync("http://www.eb80.com/login/region.aspx",content);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();

                Console.WriteLine(responseBody);

1.这里我用的httpclient,开始一直用webrequest太麻烦了,而且还出了问题没搞出结果来,看来官方文档说最好不要用那个webrequest,不然重写什么什么的方法,小菜基我不会也嫌麻烦,转而httpclient.

这里Add里的参数这几个是要加上的 ,参数值自己网页访问抓包的时候查好。下面我用的抓包软件fiddler 官网下就好:


 

2.然后还有一个点是viewstate 和eventvalidation这两个参数,要先get获取下来再post的时候要写进去。

下面代码获取这两个参数值。用正则式获取(这里内容多,不懂得学,菜鸟教程查一查知道怎么对应你的就行),这里要对应你网页返回的HTML格式://Regex reg = new Regex("<input type=\"hidden\" name=\"(.*?)\" id=\"(.*?)\" value=\"(.*?)\" />",

string re = Get("http://www.eb80.com/login/region.aspx");
            //正则匹配拿到两个参数
            string viewState = GetHiddenField(re, true);
            string eventValidation = GetHiddenField(re, false);

public static string Get(string url)
        {
            string result = "";

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.CookieContainer = new CookieContainer();
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();

            try
            {
                //获取内容
                using (StreamReader reader = new StreamReader(stream))
                {
                    result = reader.ReadToEnd();
                }

                string cookiestr = resp.Headers.Get("Set-Cookie");
                Console.WriteLine(cookiestr + "\n");
            }
            finally
            {
                stream.Close();
            }
            return result;
        }
        public static string GetHiddenField(string tempstr, bool flag)
        {
            string matchValue = string.Empty;
            //Console.WriteLine(tempstr);
            Match match;
            if (flag)
            {
                //Regex reg = new Regex("<input type=\"hidden\" name=\"(.*?)\" id=\"(.*?)\" value=\"(.*?)\" />", RegexOptions.IgnoreCase);
                string val_VIEWSTATE = string.Empty;
                Regex reg_VIEWSTATE = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" />", RegexOptions.IgnoreCase);
                match = reg_VIEWSTATE.Match(tempstr);
                if (match.Success)
                {
                    matchValue = match.Value;//匹配的字符串
                    val_VIEWSTATE = match.Groups[1].Value; //匹配的value值,正则表达式中的第一个括号
                }
                return val_VIEWSTATE;
            }
            else
            {
                string val_EVENTVALIDATION = string.Empty;
                Regex reg_EVENTVALIDATION = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" />", RegexOptions.IgnoreCase);
                match = reg_EVENTVALIDATION.Match(tempstr);
                if (match.Success)
                {
                    matchValue = match.Value;//匹配的字符串
                    val_EVENTVALIDATION = match.Groups[1].Value; //匹配的value值,正则表达式中的第一个括号
                }

                return val_EVENTVALIDATION;
            }

 

添加参数的写法:

 string userid = "asddfdf2fdss";
 string password = "123456";

 try
            {
                var content = new FormUrlEncodedContent(new[]
             {
                new KeyValuePair<string, string>("__VIEWSTATE", viewState),
                new KeyValuePair<string, string>("__EVENTVALIDATION", eventValidation),
                new KeyValuePair<string, string>("ctl00$ContentPlaceHolder1$txtUserName", userid),
                new KeyValuePair<string, string>("ctl00$ContentPlaceHolder1$txtPwd", password),

                //................

 });


        }

3.用post还是get请求,这个看抓包下来的是什么请求就用什么,因为有些网站不一样,跟着来!

 

到这里就完成了,里面有些东西自己改改就好。或者可以找我要源码。小透明求个赞!

本文地址:https://blog.csdn.net/GXU_PJM/article/details/107490934

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网