Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从异步函数中获取HTTP响应_C#_Http_Get_Request_Response - Fatal编程技术网

C# 如何从异步函数中获取HTTP响应

C# 如何从异步函数中获取HTTP响应,c#,http,get,request,response,C#,Http,Get,Request,Response,您好,我正在尝试获取此异步函数之外的HTTP响应内容,但无法返回它,因为它是异步的。我该怎么办 以下是函数: public async Task<HttpResponseMessage> GetResponse(string url) { using (HttpClient client = new HttpClient()) using (HttpResponseMessage response = await client.GetA

您好,我正在尝试获取此异步函数之外的HTTP响应内容,但无法返回它,因为它是异步的。我该怎么办

以下是函数:

    public async Task<HttpResponseMessage> GetResponse(string url)
    {
        using (HttpClient client = new HttpClient())
        using (HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false))
        using (HttpContent content = response.Content)
        {
            Console.WriteLine(content.ReadAsStringAsync().Result);
            return response;
        }
    }
公共异步任务GetResponse(字符串url) { 使用(HttpClient=new HttpClient()) 使用(httpresponsemessageresponse=await client.GetAsync(url.ConfigureAwait(false)) 使用(HttpContent=response.content) { Console.WriteLine(content.ReadAsStringAsync().Result); 返回响应; } }
不要访问
结果
,它会阻塞线程;改用
wait

Console.WriteLine(await content.ReadAsStringAsync());
要访问方法之外的内容,请执行以下操作:

var response = await GetResponse("url");
using (HttpContent content = response.Content)
{
    var contentString = await content.ReadAsStringAsync();
}
public async Task<string> GetContentAsync(string url)
{
    using (HttpClient client = new HttpClient())
    using (HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false))
    using (HttpContent content = response.Content)
    {
        return await content.ReadAsStringAsync();
    }
}
如果要返回响应,请确保不要使用块将其包装在
中,否则它将在您有机会阅读内容之前被处理:

public async Task<HttpResponseMessage> GetResponse(string url)
{
    using (HttpClient client = new HttpClient())
    {
        return await client.GetAsync(url).ConfigureAwait(false);
    }
}
公共异步任务GetResponse(字符串url) { 使用(HttpClient=new HttpClient()) { 返回await client.GetAsync(url).ConfigureAwait(false); } }
或者直接从方法返回内容:

var response = await GetResponse("url");
using (HttpContent content = response.Content)
{
    var contentString = await content.ReadAsStringAsync();
}
public async Task<string> GetContentAsync(string url)
{
    using (HttpClient client = new HttpClient())
    using (HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false))
    using (HttpContent content = response.Content)
    {
        return await content.ReadAsStringAsync();
    }
}
公共异步任务GetContentAsync(字符串url) { 使用(HttpClient=new HttpClient()) 使用(httpresponsemessageresponse=await client.GetAsync(url.ConfigureAwait(false)) 使用(HttpContent=response.content) { 返回wait content.ReadAsStringAsync(); } }
我的问题是,当我试图显示内容时,我得到的是“System.Threading.Tasks.Task`1[System.String]”,而不是响应中应该包含的字符串(如果需要,可以使用HTML或JSON)那么我如何解决这个问题,因为我需要了解我在做什么with@CSE_Random您一定错过了一个
await
,例如
string content=await-GetContentAsync(“url”)