繁体   English   中英

将WebClient转换为HttpClient

[英]Convert WebClient to HttpClient

我试图将以前在Win7项目上使用的WebClient转换为HttpClient ,以便在Win8.1系统上使用它。

WenClient:

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的第一枪

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 optionWebClient返回成功的响应。

应该怎么做?

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

UploadValues的msdn页面表示,WebClient在POST请求中使用application/x-www-form-urlencoded Content-type发送数据。 因此,您必须/可以使用FormUrlEncodedContent http内容。

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;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM