如何通过使用Http Web请求首先添加viewstate来进行发布请求

时间:2019-04-09 12:24:43

标签: post webclient

我要发出发帖请求 为了使发布请求正常工作,我需要在其他输入中传递viewstate,viewstate generator和eventvalidation。

问题在于,每次页面加载时,这些viewstate,viewstate生成器和事件验证参数都会发生变化

所以我需要一口气处理所有事情。

public static async Task<HtmlDocument> PostRequestTest(string cookie, string url)
    {


        var sb = new StringBuilder();
        AppendParameter(sb, "Textbox1", "cc");
        AppendParameter(sb, "Textbox2", "sa");
        AppendParameter(sb, "Textbox3", "Baslatan");
        AppendParameter(sb, "Button1", "Login");




        //Get html body from url and store it to htmlToParse
        string htmlToParse;

        var webRequest = WebRequest.Create(url) as HttpWebRequest;
        using (var responseReader = new StreamReader( (await webRequest?.GetResponseAsync()).GetResponseStream()))
        {
             htmlToParse = await responseReader.ReadToEndAsync();

        }

        //load Html With agility Pack
        var originalUrlDoc = new HtmlDocument();
        originalUrlDoc.LoadHtml(htmlToParse);

        //Get values and append to list
        var viewState = originalUrlDoc.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='__VIEWSTATE']").Attributes["value"].Value;
        var viewStateGenerator = originalUrlDoc.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='__VIEWSTATEGENERATOR']").Attributes["value"].Value;
        var eventValidation = originalUrlDoc.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='__EVENTVALIDATION']").Attributes["value"].Value;
        AppendParameter(sb, "__VIEWSTATE", viewState);
        AppendParameter(sb, "__VIEWSTATEGENERATOR", viewStateGenerator);
        AppendParameter(sb, "__EVENTVALIDATION", eventValidation);


        var databytes = Encoding.UTF8.GetBytes(sb.ToString());


        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8";



        webRequest.CookieContainer = new CookieContainer();
        webRequest.CookieContainer.SetCookies(new Uri(url), cookie);

        //request.AllowAutoRedirect = false;
        //request.UseDefaultCredentials = false;
        //request.ContentLength = databytes.Length;

        //Here I get an exception saying System.Net.ProtocolViolationException: 'Cannot send a content-body with this verb-type.'

        using (var stream = webRequest.GetRequestStream())
        {
            stream.Write(databytes, 0, databytes.Length);
        }



        using (var response = (HttpWebResponse)(await webRequest.GetResponseAsync()))
        {
            var responseString = new StreamReader( response.GetResponseStream()).ReadToEndAsync();

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(await responseString);

            return htmlDoc;
        }


    }
    protected static void AppendParameter(StringBuilder sb, string name, string value)
    {
        string encodedValue = HttpUtility.UrlEncode(value);
        sb.AppendFormat("{0}={1}&", name, encodedValue);
    }

所以当代码敲入

//在这里,我得到一个异常,提示System.Net.ProtocolViolationException:'无法发送具有这种动词类型的内容主体。'

    using (var stream = webRequest.GetRequestStream())
    {
        stream.Write(databytes, 0, databytes.Length);
    }

0 个答案:

没有答案