开发者

How to send an array to ActionResult using HttpWebRequest

I want to send an array of string to ActionResult from another application using HttpWebRequest with post method:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:12345/ReceiveData");
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
string data = "tags=data1&tags=data2&tags=data3";
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
request.ContentLength = dataBytes.Length;
Stream reqStream = request.GetRequestStream();
reqStream.Write(dataBytes, 0, dataBytes.Length);
reqStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Encoding encoding = Sys开发者_如何转开发tem.Text.Encoding.GetEncoding(1252);
StreamReader responseStream = new StreamReader(response.GetResponseStream(), encoding);
string stringResponse = responseStream.ReadToEnd();
responseStream.Close();
response.Close();

The ActionResult method:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ReceiveData(string[] tags)
{
    return Json(tags);
}

I recieve the data paramater with only one string containing "data1,data2,data3". How I must send the data to receive the array parameter in the correct form??? I can use split to generate the array inside the action method but I prefeer to receive the data in the correct form.


try using,

tags[]=data1&tags[]=data2&tags[]=data3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜