在 C# 中发出和接收 HTTP 请求
我想让我的 C# 应用程序能够发送 http 请求并在运行时接收答案,
我想要请求的网站的解释是 这里
我以前没有任何经验,所以我对 JSON、XML 的东西有点困惑 我知道我需要一个 XML 解析器或类似的东西来理解请求
I want to make my C# application to be able to send an http request and receive the answer at runtime
an explanation from the website I want to request from is HERE
I don't have any experience with that before, so I'm a little confused about the JSON, XML stuff
I know I'll need an XML parser or something like this to understand the request
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您不想自定义 HTTP 请求,那么发出 HTTP 请求非常简单:一种方法调用
WebClient.DownloadString
。例如:您每次都需要根据链接到的文档构建正确的 URL。
如果您使用上面的示例代码与您的 API 对话,
html
(通常实际上是响应数据)将包含 XML 或 JSON 作为字符串。然后,您需要将其解析为某种其他类型的对象树,以便您可以处理响应。Making a HTTP request is very simple if you don't want to customize it: one method call to
WebClient.DownloadString
. For example:You will need to build the correct URL each time as per the documentation you link to.
If you use the example code above to talk to your API,
html
(which is really the response data in general) will contain either XML or JSON as a string. You would then need to parse this into some other type of object tree so that you can work with the response.除了按照建议使用 WebClient 之外,您还可以查看 JetBrains 的 Hadi Hariri 开发的 EasyHttp。您可以在 https://github.com/hhariri/EasyHttp 找到它 自述文件摘要:
EasyHttp - 一个易于使用的 HTTP 客户端支持:
Apart from using WebClient as suggested, you could also have a look at EasyHttp by Hadi Hariri from JetBrains. You can find it at https://github.com/hhariri/EasyHttp Summary from ReadMe:
EasyHttp - An easy to use HTTP client that supports:
您需要查找
HttpWebRequest
和HttpWebResponse
对象。这些将是实际发出 HTTP 请求的对象。请求和响应将在您链接到的 ViralHeat API 的正文中包含 XML 和 JSON。
You'll want to look up the
HttpWebRequest
andHttpWebResponse
objects. These will be the objects that actually make the HTTP requests.The request and response will contain XML and JSON in the bodies per ViralHeat's API that you linked to.
您可以实现 WCF REST API : http://www.codeproject.com/KB/WCF /RestServiceAPI.aspx
You could implement a WCF REST API : http://www.codeproject.com/KB/WCF/RestServiceAPI.aspx
这个 http://www.nuget.org/List/Packages/HttpClient 是 Microsoft 的战略httpclient 继续前进。我希望在不久的将来看到这个库在所有 Microsoft 平台上实现。
This http://www.nuget.org/List/Packages/HttpClient is Microsoft's strategic httpclient moving forward. I Expect to see this library implemented across all of Microsoft's platforms in the near future.