将 NameValueCollection 发送到 http 请求 C#

发布于 2024-11-29 04:26:28 字数 416 浏览 0 评论 0原文

我有这种情况。 我们使用某种方法进行登录,但该方法处于更高的抽象级别,因此它只有用户名和密码等参数,并且使用此参数创建一些名称值集合,然后传递给某些请求构建器。注入这个请求构建器以便我可以更改它的实现。现在我们使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-12-06 04:26:28

我不是 100% 确定您想要什么,但要创建一个从 NameValueCollection 发布一些数据的 Web 请求,您可以使用如下内容:

HttpWebRequest GetRequest(String url, NameValueCollection nameValueCollection)
{
    // Here we convert the nameValueCollection to POST data.
    // This will only work if nameValueCollection contains some items.
    var parameters = new StringBuilder();

    foreach (string key in nameValueCollection.Keys)
    {
        parameters.AppendFormat("{0}={1}&", 
            HttpUtility.UrlEncode(key), 
            HttpUtility.UrlEncode(nameValueCollection[key]));
    }

    parameters.Length -= 1;

    // Here we create the request and write the POST data to it.
    var request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Method = "POST";

    using (var writer = new StreamWriter(request.GetRequestStream()))
    {
        writer.Write(parameters.ToString());
    }

    return request;
}

但是,您发布的数据将取决于您接受的格式。此示例使用查询字符串格式,但如果您切换到 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:

HttpWebRequest GetRequest(String url, NameValueCollection nameValueCollection)
{
    // Here we convert the nameValueCollection to POST data.
    // This will only work if nameValueCollection contains some items.
    var parameters = new StringBuilder();

    foreach (string key in nameValueCollection.Keys)
    {
        parameters.AppendFormat("{0}={1}&", 
            HttpUtility.UrlEncode(key), 
            HttpUtility.UrlEncode(nameValueCollection[key]));
    }

    parameters.Length -= 1;

    // Here we create the request and write the POST data to it.
    var request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Method = "POST";

    using (var writer = new StreamWriter(request.GetRequestStream()))
    {
        writer.Write(parameters.ToString());
    }

    return request;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文