首页 新闻 会员 周边 捐助

C#访问URL并获取文件内容

0
悬赏园豆:50 [已解决问题] 解决于 2011-09-30 15:51

C# 通过URL地址获取文件的内容

ljxhkl的主页 ljxhkl | 初学一级 | 园豆:66
提问于:2011-09-30 15:49
< >
分享
最佳答案
1

 string rXml = string.Empty;

        HttpWebRequest myHttpWebRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
        myHttpWebRequest.KeepAlive = false;
        myHttpWebRequest.AllowAutoRedirect = false;
        myHttpWebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
        myHttpWebRequest.Timeout = 10000;
        myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

        using (HttpWebResponse res = (HttpWebResponse)myHttpWebRequest.GetResponse())
        {

            if (res.StatusCode == HttpStatusCode.OK || res.StatusCode == HttpStatusCode.PartialContent)//返回为200或206
            {
                string dd = res.ContentEncoding;
                System.IO.Stream strem = res.GetResponseStream();
                System.IO.StreamReader r = new System.IO.StreamReader(strem);
                rXml = r.ReadToEnd();
            }
        }

        Response.Write(rXml);

收获园豆:50
sunlary | 小虾三级 |园豆:934 | 2011-09-30 15:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册