问题
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:
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;
来源:https://stackoverflow.com/questions/36271702/c-sharp-read-not-write-string-from-system-net-http-stringcontent