简体   繁体   中英

C# Post to browser fails, any ideas?

This is a quick and dirty app that only needs to work for a short period. I'm not a developer so please don't hammer me. The following code in asp works fine (secret info replaced with example.com and abc 123).

I know the below is very bad practice, but this is just for demonstration purpose:

<form method="post" action="https://example.com/asppage.aspx" id="frm_main">
<input type="hidden" name="STATE" id="STATE" value="ABC" />
<input type="hidden" name="VALIDATION" id="VALIDATION" value="123/>
<input type="submit" name="refresh_progress" value="Check Status" id="refresh_progress" /></form>

However, the same code in my c# post doesn't work:

string PostData = "STATE=ABC&amp;VALIDATION=123";
webBrowser1.Navigate("https://example.com/asppage.aspx", "_blank", Encoding.Default.GetBytes(PostData), "Content-Type: application/x-www-form-urlencoded\n\r");

When the new browser window pops up, its the default asppage.aspx form with no data posted to it.

Any ideas what I'm doing wrong?

You're giving the webBrowser the html of the form, the POST data is a serialised format of the names and values of the form fields which is what you need to put in your Navigate method.

The postdata needs to be in the format:

inputname1=value1&inputname2=value2&inputname3=value3

You will also need to uri encode the string and include Content-Type: application/x-www-form-urlencoded as the fourth parameter in the method call.

Your POST format is completely wrong.

See the specification .

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