简体   繁体   English

从 URL 读取到 .NET 中的字符串的最简单方法

[英]Easiest way to read from a URL into a string in .NET

Given a URL in a string:给定一个字符串中的 URL:

http://www.example.com/test.xml

What's the easiest/most succinct way to download the contents of the file from the server (pointed to by the url) into a string in C#?将文件内容从服务器(由 url 指向)下载到 C# 中的字符串中的最简单/最简洁的方法是什么?

The way I'm doing it at the moment is:我目前的做法是:

WebRequest request = WebRequest.Create("http://www.example.com/test.xml");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();

That's a lot of code that could essentially be one line:这是很多代码,基本上可以是一行:

string responseFromServer = ????.GetStringFromUrl("http://www.example.com/test.xml");

Note: I'm not worried about asynchronous calls - this is not production code.注意:我不担心异步调用——这不是生产代码。

using(WebClient client = new WebClient()) {
   string s = client.DownloadString(url);
}

The method in the above answer is now deprecated, the current recommendation is to use HttpClient:上述答案中的方法现在已弃用,目前推荐使用 HttpClient:

using (HttpClient client = new HttpClient())
{
    string s = await client.GetStringAsync(url);
}

Given that, at the time of this writing, HttpClient is the only remaining, valid .Net mechanism for performing this function, and, in any case where you're "not worried about asynchronous calls" (which appear to be unavoidable with HttpClient ), I think that this function should get you what you're after:鉴于在撰写本文时, HttpClient唯一剩余的、有效的 .Net 机制,用于执行此 function,并且在任何情况下您“不担心异步调用” (这似乎是HttpClient不可避免的) ,我认为这个 function 应该让你得到你所追求的:

public static class Http
{
    ///<remarks>NOTE: The <i>HttpCLient</i> class is <b>intended</b> to only ever be instantiated once in any application.</remarks>
    private static readonly HttpClient _client = new();

    /// <summary>Used to retrieve webserver data via simple <b>GET</b> requests.</summary>
    /// <param name="url">A string containing the complete web <b>URL</b> to submit.</param>
    /// <returns>Whatever <i>HttpClient</i> returns after attempting the supplied query (as a <i>Task&lt;string&gt;</i> value).</returns>
    /// <exception cref="InvalidOperationException">Returned if the supplied <i>url</i> string is null, empty or whitespace.</exception>
    private static async Task<string> HttpClientKludge( string url )
    {
        if ( string.IsNullOrWhiteSpace( url ) )
            throw new InvalidOperationException( "You must supply a url to interrogate for this function to work." );

        Uri uri;
        try { uri = new Uri( url ); }
        catch ( UriFormatException e ) { return $"{e.Message}\r\n{url}"; }

        return await _client.GetStringAsync( uri );
    }

    /// <summary>Attempts to interrogate a website via the supplied URL and stores the result in a <i>string</i>.</summary>
    /// <param name="url">A string containing a fully-formed, proper URL to retrieve.</param>
    /// <param name="captureExceptions">If <b>TRUE</b>, any Exceptions generated by the operation will be suppressed with their Message returned as the result string, otherwise they're thrown normally.</param>
    /// <returns>The result generated by submitting the request, as a <i>string</i>.</returns>
    public static string Get( string url, bool captureExceptions = true )
    {
        string result;
        try { result = HttpClientKludge( url ).Result; }
        catch (AggregateException e)
        {
            if (!captureExceptions) throw;
            result = e.InnerException is null ? e.Message : e.InnerException.Message;
        }
        return result;
    }
}

With that in place, anytime you want to interrogate a website with a simple URL+GET inquiry, you can simply do:有了这个,任何时候你想用一个简单的 URL+GET 查询来询问一个网站,你可以简单地做:

string query = "/search?q=Easiest+way+to+read+from+a+URL+into+a+string+in+.NET",
siteResponse = Http.Get( $"https://www.google.com{query}" );
// Now use 'siteResponse' in any way you want...

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

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