<form action="https://somewebsiteaddrest" method="post">
<input name="Var1" type="hidden" value="Variable 1 value" />
<input name="Var2" type="hidden" value="Variable 2 value" />
<input name="Var3" type="hidden" value="Variable 3 value" />
<input name="Var4" type="hidden" value="Variable 4 value" />
<input name="Var5" type="hidden" value="Variable 6 value" />
<input type="submit" value="Go now" />
</form>
我想在不使用此表单和javascript frm.submit()
的情况下创建相等的发布方法和重定向(在 C# 中) ;
我有一个 C# 代码片段,我希望能做这些事情。
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
string postData = "What to write here? here should be variable i guess.";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
string postData
在 postData 中写什么以获得与使用form
and相同的效果form.submit()
?提前致谢