• 从网络上下载数据


    /// <summary>
    /// 售房网下载数据
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string GetResponseString(string url)
    {
    string _StrResponse = "";
    HttpWebRequest _WebRequest = (HttpWebRequest)WebRequest.Create(url);
    _WebRequest.UserAgent = "MOZILLA/4.0 (COMPATIBLE; MSIE 7.0; WINDOWS NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
    _WebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;//自动解压
    _WebRequest.Method = "GET";
    WebResponse _WebResponse = _WebRequest.GetResponse();
    StreamReader _ResponseStream = new StreamReader(_WebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GBK"));
    _StrResponse = _ResponseStream.ReadToEnd();
    _WebResponse.Close();
    _ResponseStream.Close();
    return _StrResponse;
    }
    
     
    
    //发送post请求
    
    public void ProcessRequest(HttpContext context)
    
            {
     
    
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://fanyi.baidu.com/transcontent");
     
    
                Encoding encoding = Encoding.UTF8;
     
    
                string param = "ie=utf-8&source=txt&query=hello&t=1327829764203&token=8a7dcbacb3ed72cad9f3fb079809a127&from=auto&to=auto";
     
    
                //encoding.GetBytes(postData);
     
    
                byte[] bs = Encoding.ASCII.GetBytes(param);
     
    
                string responseData = String.Empty;           
     
    
                req.Method = "POST";
     
    
                req.ContentType = "application/x-www-form-urlencoded";
     
    
                req.ContentLength = bs.Length;
     
    
                using (Stream reqStream = req.GetRequestStream())//发送post请求
     
    
                {
     
    
                    reqStream.Write(bs, 0, bs.Length);
     
    
                    reqStream.Close();
     
    
                }
     
    
                using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
     
    
                {
     
    
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(),encoding))
     
    
                    {
     
    
                        responseData = reader.ReadToEnd().ToString();
     
    
                    }
     
    
                    context.Response.Write(responseData);
     
    
                }
     
    
            }
     
     
     
    //保存文件
    WebRequest request = WebRequest.Create("url地址");
    
    WebResponse response = request.GetResponse();
    Stream reader = response.GetResponseStream();
    FileStream writer = new FileStream(" 保存文件的绝对路径", FileMode.OpenOrCreate, FileAccess.Write);
    byte[] buff = new byte[512];
    int c = 0; //实际读取的字节数
    while ((c = reader.Read(buff, 0, buff.Length)) > 0)
    {
    writer.Write(buff, 0, c);
    }
    writer.Close();
    writer.Dispose();
    reader.Close();
    reader.Dispose();
    response.Close();
     
  • 相关阅读:
    权重
    盒模型
    认识html标签
    CSS盒子模型
    行内元素和块级元素的区别
    搭建线路mvc实现接口获取数据库数据
    实现车辆信息编辑功能
    最近系统更新进度截图
    最近没写什么---更新下,在家没键盘就偷懒了
    基于web公交查询系统----管理员公交站点管理页面实现
  • 原文地址:https://www.cnblogs.com/fierceeagle/p/4235950.html
Copyright © 2020-2023  润新知