C#从System.Net.Http.StringContent读取(不写入!)字符串 [英] C# Read (not write!) string from System.Net.Http.StringContent

查看:2113
本文介绍了C#从System.Net.Http.StringContent读取(不写入!)字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个貌似应该是一个简单的问题,但在任何地方都找不到答案.给出以下代码:

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 = ???;

为了从sc读取字符串值以便myContent = "Hello!",我需要用什么替换????

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

.ToString.ReadAsStringAsync一样仅返回System.String.如何读出我写的内容?

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

推荐答案

您可以使用ReadAsStringAsync()方法,然后使用await语句或Result属性获取结果:

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;

这篇关于C#从System.Net.Http.StringContent读取(不写入!)字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆