C# Read (not write!) string from System.Net.Http.S

2019-06-15 05:28发布

I have what seems like it should be a simple question, but I can't find an answer to it anywhere. Given the following code:

    using System.Net.Http;
    ...
    StringContent sc = New StringContent("Hello!");
    string myContent = ???;

What do I need to replace the ??? with in order to read the string value from sc, so that myContent = "Hello!"?

.ToString just returns System.String, as does .ReadAsStringAsync. How do I read out what I've written in?

1条回答
不美不萌又怎样
2楼-- · 2019-06-15 06:05

You can use ReadAsStringAsync() method, then get the result using await statement or Result property:

StringContent sc = new StringContent("Hello!");

string myContent = await sc.ReadAsStringAsync();
//or
string myContent = sc.ReadAsStringAsync().Result;
查看更多
登录 后发表回答