将 WebClient 转换为 HttpClient [英] Convert WebClient to HttpClient

查看:47
本文介绍了将 WebClient 转换为 HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我曾经在 Win7 项目上使用的 WebClient 转换为 HttpClient 以在我的 上使用它Win8.1系统.

I am trying to convert a WebClient I used to use on a Win7 project to a HttpClient to use it on my Win8.1 system.

文客户:

public static void PastebinSharp(string Username, string Password)
        {
            NameValueCollection IQuery = new NameValueCollection();

            IQuery.Add("api_dev_key", IDevKey);
            IQuery.Add("api_user_name", Username);
            IQuery.Add("api_user_password", Password);

            using (WebClient wc = new WebClient())
            {
                byte[] respBytes = wc.UploadValues(ILoginURL, IQuery);
                string resp = Encoding.UTF8.GetString(respBytes);

                if (resp.Contains("Bad API request"))
                {
                    throw new WebException("Bad Request", WebExceptionStatus.SendFailure);
                }
                Console.WriteLine(resp);
                //IUserKey = resp;
            }
        }

这是我第一次使用 HttpClient

And this is my first shot on HttpClient

public static async Task<string> PastebinSharp(string Username, string Password)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("api_dev_key", GlobalVars.IDevKey);
                client.DefaultRequestHeaders.Add("api_user_name", Username);
                client.DefaultRequestHeaders.Add("api_user_password", Password);

                using (HttpResponseMessage response = await client.GetAsync(GlobalVars.IPostURL))
                {
                    using (HttpContent content = response.Content)
                    {
                        string result = await content.ReadAsStringAsync();
                        Debug.WriteLine(result);
                        return result;
                    }
                }
            }
        }

我的 HttpRequest 返回 Bad API request, invalid api option 而我的 WebClient 返回一个成功的响应.

My HttpRequest returns Bad API request, invalid api option while my WebClient returns a successful response.

这应该怎么做?

我当然明白我添加的是标题而不是查询,但我不知道如何添加查询...

I understand of course I am adding headers instead of query, but I have no idea how to add queries...

推荐答案

UploadValues 表示 WebClient 在带有 application/x-www-form-urlencoded 内容类型的 POST 请求中发送数据.所以你必须/可以使用 FormUrlEncodedContent http 内容.

The msdn page of UploadValues say that WebClient send data in a POST request with application/x-www-form-urlencoded Content-type. So you must/can use FormUrlEncodedContent http content.

public static async Task<string> PastebinSharpAsync(string Username, string Password)
{
    using (HttpClient client = new HttpClient())
    {
        var postParams = new Dictionary<string, string>();

        postParams.Add("api_dev_key", IDevKey);
        postParams.Add("api_user_name", Username);
        postParams.Add("api_user_password", Password);

        using(var postContent = new FormUrlEncodedContent(postParams))
        using (HttpResponseMessage response = await client.PostAsync(ILoginURL, postContent))
        {
            response.EnsureSuccessStatusCode(); // Throw if httpcode is an error
            using (HttpContent content = response.Content)
            {
                string result = await content.ReadAsStringAsync();
                Debug.WriteLine(result);
                return result;
            }
        }
    }
}

这篇关于将 WebClient 转换为 HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆