奇怪格式的C#输出

时间:2018-10-09 12:27:59

标签: c# encoding

我试图从CURL请求中获取输出已经有几个星期了。

static void Main(string[] args)
{
    using (var httpClient = new HttpClient())
    {
        using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://example/Login"))
        {
            request.Headers.TryAddWithoutValidation("Accept", "application/json");

            request.Content = new StringContent("{\"username\":\"username\",\"password\":\"password\"}", Encoding.UTF8, "application/json");

            var task = httpClient.SendAsync(request);
            task.Wait();
            var response = task.Result;

            Console.WriteLine(response);
            Console.ReadKey();
        }
    }
}

从这里我得到以下输出:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Date: Tue, 09 Oct 2018 12:23:28 GMT
  Date: Tue, 09 Oct 2018 12:23:29 GMT
  Connection: close
  Set-Cookie: TS0119a36f=015d5689807756bd7792c622b0d146d50f8131b81e7cfa77d76c0b50114e2d09340d72b8f8ea0462c3476511b8474077967ae579e7; Path=/; Domain=.example.com
  Transfer-Encoding: chunked
  Content-Type: application/json
}

但是当我从cmd执行CURL时,我得到了(也是我从c#中需要的东西)

  

{“代码”:200,“状态”:“成功”,“ sessionId”:“ 35aaf800-bfdb-11e8-991a-d35512019464”}

我的猜测是我的编码错误。但我不知道..

1 个答案:

答案 0 :(得分:0)

谢谢男孩,你是最棒的

Console.WriteLine(response.Content.ReadAsStringAsync().Result);

为我工作