简体   繁体   English

发送Asp.net HttpClient获取结果到浏览器

[英]Send Asp.net HttpClient Get result to browser

I have an asp.net mvc controller method "Index" as showed below. 我有一个asp.net mvc控制器方法“索引”,如下所示。 I want to use HttpClient GetAsync method to get a response from wwww.google.com and then send this response as the response to browser, so browser shows www.google.com. 我想使用HttpClient GetAsync方法从wwww.google.com获取响应,然后将此响应作为响应发送到浏览器,因此浏览器显示www.google.com。 But I do not know how to replace response with the response I got from client.GetAsync. 但是我不知道如何用从client.GetAsync获得的响应替换响应。 Please help! 请帮忙! I don't want to use redirect though. 我不想使用重定向。

    public async Task<ActionResult> Index()
    {
        HttpClient client = new HttpClient();
        var httpMessage = await client.GetAsync("http://www.google.com");


        return ???;
    }

You can read the response from the http call as a string using the ReadAsStringAsync() method and return that string back as the response from your action method. 您可以使用ReadAsStringAsync()方法将http调用的响应作为字符串ReadAsStringAsync() ,并将该字符串作为action方法的响应返回。

public async Task<ActionResult> Index()
{
    HttpClient client = new HttpClient();
    var httpMessage = await client.GetAsync("http://www.google.com");

    var r = await httpMessage.Content.ReadAsStringAsync();

    return Content(r);
}

The above code will return you the content of google.com when you access the Index action. 当您访问Index操作时,上面的代码将返回google.com的内容。

please try to see this post HttpClient Request like browser 请尝试像浏览器一样查看这篇文章HttpClient Request

response.EnsureSuccessStatusCode();
    using (var responseStream = await response.Content.ReadAsStreamAsync())
    using (var decompressedStream = new GZipStream(responseStream, CompressionMode.Decompress))
    using (var streamReader = new StreamReader(decompressedStream))
    {
        return streamReader.ReadToEnd();
    }

I make sample on my machine 我在机器上做样品

full action here 在这里全力以赴

    public async Task<string> Index()
    {
        HttpClient client = new HttpClient();


        client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml");
        client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
        client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
        client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");


        var httpMessage = await client.GetAsync("http://www.google.com");

        httpMessage.EnsureSuccessStatusCode();
        using (var responseStream = await httpMessage.Content.ReadAsStreamAsync())
        using (var decompressedStream = new GZipStream(responseStream, CompressionMode.Decompress))
        using (var streamReader = new StreamReader(decompressedStream))
        {
            return streamReader.ReadToEnd();
        }
        // return View();
    }

example for get cookie from server 从服务器获取Cookie的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM