c# - 从 Windows 窗体 C# 发送发布请求

标签 c# curl post windows-forms-designer

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

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

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

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

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

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

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

最佳答案

        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

                    }
                }

关于c# - 从 Windows 窗体 C# 发送发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45429934/

相关文章:

c# - Wix 使用编辑控件

c# - C# 中的 VBA/Excel RANK

c# - 无法弄清楚这些 C# 和 Java 代码的不同之处

r - 使用带有 cURL、RCurl 和 httr 的 cookie 发布请求

PHP Curl 未加载

php - http post方法是如何实现的?

c# - 版本控制,如何构建具有唯一 publicKeyTokens 的各种程序集版本?

php - 如何在 rest API 中传递 token ?

javascript - 如何确定使用 $.post() 或 $.get() 时的错误

php - AJAX使用$_POST方法传输大量数据