使用带有一些斜线的HttpClient,有些没有

时间:2016-10-11 07:20:37

标签: c# dotnet-httpclient

我需要从c#中的url中获取json。问题是有些斜线是逃逸的,有些则不是。这是couchbase所必需的。我无法弄清楚如何做到这一点。

url:http://10.0.0.4:8091/pools/default/buckets/default/stats/replications%2Ff4268e62702130298bf87f17cc481219%2Fdefault%2Fhub%2Fdocs_failed_cr_source

couchbase docs: http://developer.couchbase.com/documentation/server/current/rest-api/rest-xdcr-statistics.html

我的代码:

        using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(username, password) })
        {
            using (var client = new HttpClient(handler))
            {
                client.BaseAddress = new Uri(url);

                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync(query);
                if (response.IsSuccessStatusCode)
                {
                    var info = await response.Content.ReadAsStringAsync();
                    return info;
                }
            }
        }

0 个答案:

没有答案