Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发送和接收post变量_C#_Asp.net_Post_Get - Fatal编程技术网

C# 发送和接收post变量

C# 发送和接收post变量,c#,asp.net,post,get,C#,Asp.net,Post,Get,我正在尝试向重定向到的url发送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 a

我正在尝试向重定向到的url发送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"];
[HttpPost]
    [Route("yourURI")]
    public void receivePost([FromBody]dynamic myObject)
    {
        //..
    }
然后像这样检索它:

// 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"];
[HttpPost]
    [Route("yourURI")]
    public void receivePost([FromBody]dynamic myObject)
    {
        //..
    }
如何将代码转换为使用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控制器来接收结果,如下所示:

// 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"];
[HttpPost]
    [Route("yourURI")]
    public void receivePost([FromBody]dynamic myObject)
    {
        //..
    }
然而,您可能还想研究使用307 re direct,特别是如果这是一个临时解决方案

职位


我个人的选择是Restsharp,它速度很快,但对于基本操作,您可以使用此

尝试使用webclient类并提出请求。@Piyushhatri您能给我一个示例吗请查看此链接,谢谢!但是我怎么寄呢?当浏览器到达重定向页面时,如何检索帖子内容?我有点误解,上面的方法只是从一个位置发送并接收结果。现在更新。安装nuget包,然后添加对
System.Net.Assembly
的引用。我也不能评论Biswas的另一个答案,但是Get和Post不是这样工作的!你不能从一端发布,也不能从另一端获取,它们是两个独立的协议!GET方法仍然发送请求,只是它不包含包含内容的主体。您是否将其安装在解决方案中的正确项目中?链接中描述的方法是引用HttpClient的正确方法,因此您一定是做错了什么…@A但有一件事我不明白。。如何使用给定的post变量重定向到给定的页面,并在重定向的页面中检索它们?只需在您希望获取数据的页面中使用“get”代码,您的数据将保存在响应页面中。该页面显示:“找不到类型或命名空间'HttpClient'。。我尝试导入名称空间“System.Net.Http”,但找不到Http。。有解决方案吗?使用System.Net.Http添加此