我正在尝试将 post 变量发送到我要重定向到的网址。
我目前正在使用 Get 方法并按如下方式发送:
// Redirect to page with url parameter (GET)
Response.Redirect("web pages/livestream.aspx?address="+ ((Hardwarerecorders.Device)devices[arrayIndex]).streamAddress);
并像这样检索它:
// Get the url parameter here
string address = Request.QueryString["address"];
如何将我的代码转换为使用 POST 方法?
B.T.W.,我不想使用表单来发送 post 变量。
最佳答案
使用HttpClient:
发送 POST 查询:
using System.Net.Http;
public string sendPostRequest(string URI, dynamic content)
{
var client = new HttpClient();
client.BaseAddress = new Uri("http://yourBaseAddress");
var valuesAsJson = JsonConvert.SerializeObject(content);
HttpContent contentPost = new StringContent(valuesAsJson, Encoding.UTF8, "application/json");
var result = client.PostAsync(URI, contentPost).Result;
return result.Content.ReadAsStringAsync().Result;
}
其中“client.PostAsync(URI, contentPost)”是将内容发送到其他网站的位置。
在另一个网站上,需要建立一个API Controller 来接收结果,如下所示:
[HttpPost]
[Route("yourURI")]
public void receivePost([FromBody]dynamic myObject)
{
//..
}
但是,您可能还想考虑使用 307 重定向,特别是如果这是临时解决方案。
关于c# - 发送和接收 post 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35939024/