简体   繁体   English

C#发布到浏览器失败,有什么想法吗?

[英]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). ASP中的以下代码可以正常工作(秘密信息已替换为example.com和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: 但是,我的C#帖子中的相同代码不起作用:

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. 当新的浏览器窗口弹出时,它是默认的asppage.aspx表单,没有数据发布到该表单。

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. 您正在向webBrowser提供表单的html,POST数据是表单字段名称和值的序列化格式,这是您需要在Navigate方法中放入的内容。

The postdata needs to be in the format: postdata必须采用以下格式:

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. 您还需要对字符串进行uri编码,并将Content-Type: application/x-www-form-urlencoded为方法调用中的第四个参数。

Your POST format is completely wrong. 您的POST格式完全错误。

See the specification . 请参阅规格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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