httpClient 200响应但是没有内容?

时间:2019-07-04 12:52:37

标签: c# rest jira response dotnet-httpclient

我正在使用httpClient访问具有Jira的json数据的URL。我已经测试过访问页面,并且HttpResponseMessage返回200代码。

当我直接转到url并登录时,也会解析内容。

但是,当我尝试读取响应正文时,我什么也没得到,因为它是空的。我不确定自己在做什么错!我真的很受困扰,因此不胜感激!

底部的初始尝试-我也删除了.Result,并尝试仅使用像这样的GetStringAsync响应,但没有任何乐趣-response2仍然为空:(

        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{token}")));
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync(uri).Result; 


        var response2 = await client.GetStringAsync(uri);


        //Checking that the response is returning a 200
        if (response.IsSuccessStatusCode)
        {
            resultsTest.InnerHtml += "Success :)" + response.ToString();

        }
        else
        {
            resultsTest.InnerHtml += "Failed :( ";
        }

由于以下注释而导致更改之前的初始代码: 我试图以三种不同的方式(在注释中标记!)阅读响应正文:

    protected void Page_Load(object sender, EventArgs e)
    {
        testAsync();
    }

    protected async System.Threading.Tasks.Task testAsync()
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        //var confString = "https://myUrl.co.uk/rest/api/content?expand=body.view&limit=500&start=1&type=page";
        var uriString = "https://myUrl.co.uk/rest/api/2/search?jql=project=RES&fields=key,summary&maxResults=1000&startAt=0";
        var uri = new Uri(uriString);

        var username = "bob";
        var token = "bob";

        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{token}")));
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync(uri).Result; 

        //First attempt to read content body
        string res = "";
        using (HttpContent content = response.Content)
        {
            // ... Read the string.
            Task<string> result = content.ReadAsStringAsync();
            res = result.Result;
            resultsTest.InnerHtml += "------";
            resultsTest.InnerHtml += res;

        }

        //Second attempt to get the body to return as a string: 
        var contents = response.Content.ReadAsStringAsync().Result;
        resultsTest.InnerHtml += contents.ToString();

        //Third attempt to get the body of the response
        using (HttpContent content = response.Content)
        {
            // ... Read the string.
            string result = await content.ReadAsStringAsync();

            // ... Display the result.
            if (result != null &&
                result.Length >= 50)
            {
                Console.WriteLine(result.Substring(0, 50) + "...");
                resultsTest.InnerHtml += "*************"; 
            }
        }




        //Checking that the response is returning a 200
        if (response.IsSuccessStatusCode)
        {
            resultsTest.InnerHtml += "Success :)" + response.ToString();

        }
        else
        {
            resultsTest.InnerHtml += "Failed :( ";
        }

    }

0 个答案:

没有答案