从 Windows Form C# 发送 Post 请求

bla*_*ury 3 c# post curl windows-forms-designer

目前我通过 curl 命令将信息传递给 Web API,如下所示:

curl -d 'info={ "EmployeeID": [ "1234567", "7654321" ], "Salary": true, "BonusPercentage": 10}' http://example.com/xyz/php/api/createjob.php
Run Code Online (Sandbox Code Playgroud)

这将返回另一个指向 API 的 URL,在此处发布所有信息:

http://example.com/xyz#newjobapi:id=19

我正在尝试通过 C# Windows 表单复制此过程,用户将在其中输入所需的信息,一旦提交,他们应该获得返回的 URL。

我已经为用户创建了输入这些信息的界面。但我不确定如何将此信息发布到 Web API 并获取生成的 url

是否有任何库可用于通过 Windows Form 复制上述 curl 过程?

Leo*_*kan 5

        HttpWebRequest webRequest;

        string requestParams = ""; //format information you need to pass into that string ('info={ "EmployeeID": [ "1234567", "7654321" ], "Salary": true, "BonusPercentage": 10}');

                webRequest = (HttpWebRequest)WebRequest.Create("http://example.com/xyz/php/api/createjob.php");

                webRequest.Method = "POST";
                webRequest.ContentType = "application/json";

                byte[] byteArray = Encoding.UTF8.GetBytes(requestParams);
                webRequest.ContentLength = byteArray.Length;
                using (Stream requestStream = webRequest.GetRequestStream())
                {
                    requestStream.Write(byteArray, 0, byteArray.Length);
                }

                // Get the response.
                using (WebResponse response = webRequest.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        StreamReader rdr = new StreamReader(responseStream, Encoding.UTF8);
                        string Json = rdr.ReadToEnd(); // response from server

                    }
                }
Run Code Online (Sandbox Code Playgroud)