将 NameValueCollection 发送到 http 请求 C#
我有这种情况。 我们使用某种方法进行登录,但该方法处于更高的抽象级别,因此它只有用户名和密码等参数,并且使用此参数创建一些名称值集合,然后传递给某些请求构建器。注入这个请求构建器以便我可以更改它的实现。现在我们使用 POST 请求,但将来我们可能会使用 XML 或 JSON,因此只需切换注入接口的实现即可。
问题是我无法罚款任何将使我 System.Net.HttpWebRequest 脱离此名称值集合的库。 我需要具有这样的原型的方法:
WebRequest / HttpWebRequest CreateRequest(Uri / string, nameValueCollection);
或者如果没有类似的东西,那么完成所有工作(发送请求、接收响应和解析它们)的库也将很好。但它需要是异步的。
提前致谢。
I have this situation.
We're using some method for login, but that method is on some higher abstraction level so it only have parameters like username and password and and that make some Name value collection with this params and than that passes to some request builder. This request builder is injected so that I can change it's implementation. Now we're using POST request, but in future we might use XML or JSON so will just switch the implementation of injected interface.
The problem is that I cannot fine any library which will make me System.Net.HttpWebRequest out of this name value collection.
I need method with prototype like this:
WebRequest / HttpWebRequest CreateRequest(Uri / string, nameValueCollection);
Or if there is no something like that, the library that does all the work (sending requests, receiving responses and parsing them) will be good too. But it needs to be async.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是 100% 确定您想要什么,但要创建一个从 NameValueCollection 发布一些数据的 Web 请求,您可以使用如下内容:
但是,您发布的数据将取决于您接受的格式。此示例使用查询字符串格式,但如果您切换到 JSON 或其他格式,则只需更改处理
NameValueCollection
的方式。I'm not 100% sure what you want, but to create a web request that will post some data from a NameValueCollection, you can use something like this:
However, the data you post will depend upon the format you accept. This example uses query string format, but if you switch to JSON or something else you just need to change the way you process the
NameValueCollection
.