2011-04-20 16 views
10

我必須做一個重定向併發送到另一個頁面變量的值ap。我不能使用GET方法,如:http://urlpage?a=1&p=2。我必須用post方法發送它們。我怎樣才能發送他們,而不使用從C#表單?我該如何做一個重定向post變量

+0

Asp.Net? MVC?你在用什麼? – 2011-04-20 09:18:36

+0

我使用的是asp.net 4.0 – 2011-04-21 08:23:10

+0

你能接受還是寫下你的答案? – 2014-10-22 19:56:51

回答

0

使用WebClient.UploadStringWebClient.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); 

更多:http://www.daveamenta.com/2008-05/c-webclient-usage/

+6

這不是一個重定向 – 2011-04-20 08:49:30

+0

你必須檢查你的答案 – 2014-10-22 19:39:12

0

此鏈接解釋你如何做到以下幾點? 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] 
+4

在網頁中,你肯定**不**需要一個Messagebox.Show ... – 2011-04-20 09:00:29

5

該類包裝形式。一種哈克,但它的作品。只需將帖子值添加到類中並調用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(); 
    } 
} 
+0

這是好的,但你應該'HttpUtility.HtmlEncode'來逃避你的表單參數在構建HTML時。 – dana 2015-09-21 16:43:40

+0

這解決了我的問題。好的解決方案 – Kevin 2016-02-04 14:56:59