zoukankan      html  css  js  c++  java
  • http方式获取远程文件内容

        public class HttpServer
        {
            /// <summary>
            /// 读取远程文件的内容
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public string ReadFromFile(string serverFilePath)
            {
                if (string.IsNullOrEmpty(serverFilePath))
                    return "";
    
                string ret = "";
                HttpWebRequest req = null;
                HttpWebResponse resp = null;
    
                try
                {
                    req = (HttpWebRequest)HttpWebRequest.Create(serverFilePath);
                    req.Method = "GET";
                    req.KeepAlive = false;
    
                    resp = req.GetResponse() as HttpWebResponse;
                    using (Stream stream = resp.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            ret = reader.ReadToEnd();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (req != null)
                        req.Abort();
                    if (resp != null)
                        resp.Close();
                }
    
                return ret;
            }
    
            /// <summary>
            /// 读取远程文件的内容(异步)
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public async Task<string> ReadFromFileAsync(string serverFilePath)
            {
                if (string.IsNullOrEmpty(serverFilePath))
                    return "";
    
                string ret = "";
                HttpWebRequest req = null;
                HttpWebResponse resp = null;
    
                try
                {
                    req = (HttpWebRequest)HttpWebRequest.Create(serverFilePath);
                    req.Method = "GET";
                    req.KeepAlive = false;
    
                    resp = req.GetResponse() as HttpWebResponse;
                    using (Stream stream = resp.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            ret = await reader.ReadToEndAsync();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (req != null)
                        req.Abort();
                    if (resp != null)
                        resp.Close();
                }
    
                return ret;
            }
        }
  • 相关阅读:
    Spark SQL join的三种实现方式
    BitMap算法
    mysql索引建立原则
    Java常用容器对比
    如何正确的停止一个线程
    读书笔记——凤凰架构
    Linux command 整理
    禁足期间,好好学习,好好休息
    Oracle SQL Update
    Shell 知识总结
  • 原文地址:https://www.cnblogs.com/yaosj/p/11125565.html
Copyright © 2011-2022 走看看