C# Post to browser fails, any ideas?
This is a q开发者_如何学编程uick 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&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.
精彩评论