2017-05-25 24 views
0

這是我有的代碼,但我只發送字符串,我該如何改變它發送一個FormCollection?如何在C#中的POST方法發送一個FormCollection?

public String peticionUrlPost_2(String url, FormCollection parametro) 
{ 
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url); 
HttpWRequest.Credentials = CredentialCache.DefaultCredentials; 
HttpWRequest.Headers.Set("Pragma", "no-cache"); 
HttpWRequest.Timeout = 300000; 
HttpWRequest.Method = "POST"; 
HttpWRequest.ContentType = "application/x-www-form-urlencoded"; 

byte[] PostData = System.Text.Encoding.ASCII.GetBytes(parametro); 

HttpWRequest.ContentLength = PostData.Length; 
Stream tempStream = HttpWRequest.GetRequestStream(); 
tempStream.Write(PostData, 0, PostData.Length); 

HttpWebResponse response = (HttpWebResponse)HttpWRequest.GetResponse(); 
Stream receiveStream = response.GetResponseStream(); 

StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); 
String respuesta = readStream.ReadToEnd(); 
tempStream.Close(); 

return respuesta; 

}

+0

這是法[HttpPost]一個控制器的動作? –

+0

如果我想要做的呼叫是由http post –

回答

1

它其實很簡單,如果你使用下面的Razor視圖引擎爲例:

@using (Html.BeginForm("peticionUrlPost_2", "AnyControllerName", FormMethod.Post)) 
{ 

    //Your all form fields like below 
    @Html.TextBoxFor(m => m.StringData) 
    @Html.CheckBoxFor(model => model.data) 
    @Html.TextBoxFor(model => model.Data) 

    <input type="submit" value="Submit strongly type synchronous form" /> 
} 

有用鏈接:https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx

希望以上信息對您有所幫助,請讓我知道您的想法或反饋

Th anks

KARTHIK

+0

但是,如果我做,如果FormCollection和作爲參數到達的方法,並且只發送POST –

+0

是的,它將發送所有控件值作爲表單集合與http post 。此外,您還可以發送路線值。還有一件事情在接收表單集合的action方法中提及[HttpPost] –

相關問題