gpt4 book ai didi

c# - 发布方法并使用某些值重定向而不使用 Form C#

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:11:14 25 4
gpt4 key购买 nike

<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();

的情况下创建相等的 post 方法和重定向(在 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 中写入什么才能获得与使用 formform.submit() 相同的效果?提前致谢

最佳答案

发布数据应该是 URL 编码的表单数据,即由 & 字符分隔的键=值对。使用 UrlEncode method对值进行编码:

string postData =
"Var1=" + HttpUtility.UrlEncode("Variable 1 value")+
"&Var2=" + HttpUtility.UrlEncode("Variable 2 value")+
"&Var3=" + HttpUtility.UrlEncode("Variable 3 value")+
"&Var4=" + HttpUtility.UrlEncode("Variable 4 value")+
"&Var5=" + HttpUtility.UrlEncode("Variable 6 value");

关于c# - 发布方法并使用某些值重定向而不使用 Form C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13173354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com