C# Post to browser fails, any ideas?

孤人 提交于 2019-12-25 03:11:59

问题


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?


回答1:


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.




回答2:


Your POST format is completely wrong.

See the specification.



来源:https://stackoverflow.com/questions/4015000/c-sharp-post-to-browser-fails-any-ideas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!