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

ぐ巨炮叔叔 提交于 2019-12-23 07:26:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!