如何使用 WebRequest 来 POST 一些数据并读取响应?
需要让服务器向 API 发送 POST,如何将 POST 值添加到 WebRequest 对象以及如何发送它并获取响应(它将是一个字符串)?
我需要发布两个值,有时甚至更多,我在这些示例中看到它说 string postData =“a string to post”;但是我如何让我发布的内容知道有多个表单值?
Need to have the server make a POST to an API, how do I add POST values to a WebRequest object and how do I send it and get the response (it will be a string) out?
I need to POST TWO values, and sometimes more, I see in these examples where it says string postData = "a string to post"; but how do I let the thing I am POSTing to know that there is multiple form values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 MSDN
请考虑到该信息必须以 key1=value1&key2=value2 的格式发送
From MSDN
Take into account that the information must be sent in the format key1=value1&key2=value2
这对我有用。我确信它可以改进,所以请随时提出建议或编辑以使其更好。
Here's what works for me. I'm sure it can be improved, so feel free to make suggestions or edit to make it better.
下面是使用 HttpWebRequest 和 HttpWebResponse 对象发布到 Web 服务的示例。
Here's an example of posting to a web service using the HttpWebRequest and HttpWebResponse objects.
可以在这里找到更强大和灵活的示例: 带有表单字段、cookie 和标头的 C# 文件上传
A more powerful and flexible example can be found here: C# File Upload with form fields, cookies and headers
下面的代码从文本文件中读取数据并将其发送到处理程序进行处理,并从处理程序接收响应数据并读取数据并将数据存储在字符串构建器类中
Below is the code that read the data from the text file and sends it to the handler for processing and receive the response data from the handler and read it and store the data in the string builder class