简体   繁体   中英

How to pull value from hidden input field

I have the following code that does a POST to a website:

private static void Submit()
    {

        string formUrl = "mywebsite";

        HttpWebRequest req = (HttpWebRequest) WebRequest.Create(formUrl);
        req.CookieContainer = _cookieContainer;
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";

        string token = string.Empty;

        string formParams =
            string.Format("formParams");
        byte[] bytes = Encoding.ASCII.GetBytes(formParams);
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);

        }
        WebResponse resp = req.GetResponse();
    }

What returns on the response is this:

<input type='hidden' name ='FW_REQUEST_ID_TOKEN' value =  '1446493462417_59.16698819328226' />

What I need to do is extract the value of that hidden input, in other words the "1446493...." so that I can use that information to make another POST. How can I extract it?

If you would consider using a DOM Parser. You can install HTML Agility Pack from here

Once installed you can load the response in the HTML Document.

string htmlResult = client.UploadString("URL", "POST", "data to post");
var doc = new HtmlDocument();
doc.LoadHtml(htmlResult);

Find your input by Id after getting all inputs.

var inputTags = doc.DocumentNode.Descendants("input").ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM