我必須做一個重定向併發送到另一個頁面變量的值a
和p
。我不能使用GET方法,如:http://urlpage?a=1&p=2
。我必須用post方法發送它們。我怎樣才能發送他們,而不使用從C#表單?我該如何做一個重定向post變量
回答
使用WebClient.UploadString
或WebClient.UploadData
您可以輕鬆地將數據發送到服務器。我將使用UploadData顯示一個示例,因爲UploadString的使用方式與DownloadString相同。
byte[] bret = client.UploadData("http://www.website.com/post.php", "POST",
System.Text.Encoding.ASCII.GetBytes("field1=value1&field2=value2"));
string sret = System.Text.Encoding.ASCII.GetString(bret);
這不是一個重定向 – 2011-04-20 08:49:30
你必須檢查你的答案 – 2014-10-22 19:39:12
此鏈接解釋你如何做到以下幾點? http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
using System.Net;
...
string HttpPost (string uri, string parameters)
{
// parameters: name1=value1&name2=value2
WebRequest webRequest = WebRequest.Create (uri);
//string ProxyString =
// System.Configuration.ConfigurationManager.AppSettings
// [GetConfigKey("proxy")];
//webRequest.Proxy = new WebProxy (ProxyString, true);
//Commenting out above required change to App.Config
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes (parameters);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; //Count bytes to send
os = webRequest.GetRequestStream();
os.Write (bytes, 0, bytes.Length); //Send it
}
finally
{
if (os != null)
{
os.Close();
}
}
try
{ // get the response
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{ return null; }
StreamReader sr = new StreamReader (webResponse.GetResponseStream());
return sr.ReadToEnd().Trim();
}
return null;
} // end HttpPost
[edit]
在網頁中,你肯定**不**需要一個Messagebox.Show ... – 2011-04-20 09:00:29
該類包裝形式。一種哈克,但它的作品。只需將帖子值添加到類中並調用post方法即可。
public class RemotePost
{
private Dictionary<string, string> Inputs = new Dictionary<string, string>();
public string Url = "";
public string Method = "post";
public string FormName = "form1";
public StringBuilder strPostString;
public void Add(string name, string value)
{
Inputs.Add(name, value);
}
public void generatePostString()
{
strPostString = new StringBuilder();
strPostString.Append("<html><head>");
strPostString.Append("</head><body onload=\"document.form1.submit();\">");
strPostString.Append("<form name=\"form1\" method=\"post\" action=\"" + Url + "\" >");
foreach (KeyValuePair<string, string> oPar in Inputs)
strPostString.Append(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", oPar.Key, oPar.Value));
strPostString.Append("</form>");
strPostString.Append("</body></html>");
}
public void Post()
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write(strPostString.ToString());
System.Web.HttpContext.Current.Response.End();
}
}
- 1. Web.go我該如何做http重定向?
- 2. 我該如何做一個301重定向從https到https?
- 3. 我如何在一些元數據上做一個wordpress post 302重定向?
- 4. 我該如何重定向?
- 5. 如何做一個目錄重定向
- 6. 我該如何做301重定向,因爲重複的網址
- 7. 301重定向基於變量在POST
- 8. 從POST獲取變量並重定向
- 9. 重定向時POST變量到另一個頁面 - php
- 10. 我想根據URL中的變量重定向,我該如何在PHP中做到這一點?
- 11. 我該如何乘以一個變量?
- 12. 將STDERR重定向到一個變量
- 13. 重定向至一個變量在PHP
- 14. 我該如何做一個POST的png骰子的價值?
- 15. 我如何做一個動態子域名重定向
- 16. node.js的玉石 - 做一個POST請求,沒有重定向
- 17. 我該如何編寫一個RewriteRule來重定向
- 18. 做post請求時無法重定向
- 19. $ .post不斷重定向我
- 20. 我想發送一個變量到xxx.php我該怎麼做?
- 21. 如何做到這一點重定向?
- 22. 發佈一個行唯一的變量,我該怎麼做?
- 23. 如何做301重定向?
- 24. 重定向變量
- 25. 如何做一個htaccess modrewrite重定向一個url?
- 26. 我該如何發送一個Javascript變量給php變量?
- 27. 想不通,我應該如何定義一個變量
- 28. 如果POST變量等於然後重定向
- 29. 如何笨重定向多變量
- 30. 如何重定向該URL?
Asp.Net? MVC?你在用什麼? – 2011-04-20 09:18:36
我使用的是asp.net 4.0 – 2011-04-21 08:23:10
你能接受還是寫下你的答案? – 2014-10-22 19:56:51