过帐方法并使用C#的某些值进行重定向 [英] Post Method and Redirect With Some Values Without Form C#
本文介绍了过帐方法并使用C#的某些值进行重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<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()
;
I want to create equal post method and redirection (in C#) without using this form and javascript frm.submit()
;
我有一个C#代码段,希望可以完成.
I've a C# code snippet which I expect to do the stuff.
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
用form
和form.submit()
在postData中写什么才能获得与原来相同的效果?预先感谢
what to write in postData to get the same effect as it will be by using form
and form.submit()
? Thanks in advance
推荐答案
发布数据应为URL编码的表单数据,即键=值对,以&
个字符分隔.使用 UrlEncode
方法对值进行编码:
The post data should be URL-encoded form data, i.e. key=value pairs separated by &
characters. Use the UrlEncode
method to encode the values:
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#的某些值进行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文