简体   繁体   中英

C# HTTPClient Form Post and Submit Issue

I am trying to do some light screen scraping of a php site with a simple form to simplify a research portion of my work. The page has a simple form with three fields, and a submit button.

When I run my code the original page is returned as if I submitted nothing. I also tried to paste in the the fields as a ?name= etc... after the php url but that also returns me to the original page as if I had not pressed the submit button.

Here is my current code:

private async void postRequest(string url)
    {
        using (HttpClient client = new HttpClient())
        {
            var values = new List<KeyValuePair<string, string>>();
            values.Add(new KeyValuePair<string, string>("case_type", "55"));
            values.Add(new KeyValuePair<string, string>("petname", "singh"));
            values.Add(new KeyValuePair<string, string>("year", "2016"));
            values.Add(new KeyValuePair<string, string>("button", "Submit"));

            var content = new FormUrlEncodedContent(values);

            var response = await client.PostAsync(url, content);
            var responseString = await response.Content.ReadAsStringAsync();

            using (StreamWriter post = new StreamWriter("post_website.txt"))
            {
                post.WriteLine(responseString);
            }
        }
    }

Chrome Dev Tools screenshot after submit . I redacted parts of the url, it's a public information site, but being safe just in case.

I assume I am doing something wrong since I have basically taught myself C#, and this is my first time trying to do anything web related with C#.

I found a similar issue on StackOverflow here: Fill Form C# & Post Error , but this code didn't work either and I don't believe a cookie is needed. I have tried various other StackOverflow answers regarding using HttpClient and WebClient POST requests to the server, but none have worked.

Thanks in advance for any suggestions or insight.

lc put me onto the right path. I made some bad assumptions about what was receiving the form input. It's obvious now I should have tried the results page url as well. Time to go learn more php and web design I think.

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