有关使用HttpWebRequest下载文件的问题 [英] question regarding downloading files using HttpWebRequest

查看:78
本文介绍了有关使用HttpWebRequest下载文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友

我在这里发布我的代码,请大家建议我哪里错了
此代码读取.doc文件对.rtf文件的响应流,它给出文档服务器正忙,请稍后再尝试".
请帮帮我

hi friends

here i am posting my code please guys suggest me where i was wrong
this code reads the response stream for .doc files for .rtf files it gives "Document server is busy please try after some time".
please help me guys

HttpWebResponse response = null;
HttpWebRequest request = null;
CookieContainer container = new CookieContainer();
//container contains all the cookies related to my site
 request = (HttpWebRequest)WebRequest.Create("http://www.mysite.com/a/test.html?id=2&cid=5");

                request.CookieContainer = new CookieContainer();
                request.CookieContainer = container;
                request.Method = "GET";
                request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
                request.KeepAlive = false;
                request.Timeout = 5000;
                request.Proxy = null;
               
                request.ContentType = "application/ms-word";

                request.Credentials = new NetworkCredential("admin", "password");
                using (response = (HttpWebResponse)request.GetResponse())
                {


                    readStream = response.GetResponseStream();
                    contentType = response.ContentType;

                    path = "E:\\Files";
string extension=string.empty;
                   if (contentType == "text/html")
                    {
                        if (request != null)
                            request = null;
                        return false;

                    }
                    else
                    {
                        if (contentType == "application/rtf")
                        {
                            extension = ".rtf";
                        }
                        else
                        {
                            extension = ".doc";
                        }


                        FileStream writeStream = new FileStream(path + extension, FileMode.Create, FileAccess.Write);
                        ReadWriteStream(readStream, writeStream);//creates the file and writes data to file.

                        if (request != null)
                            request = null;
                        return true;
                    }
                }



在上面的示例中,.doc文件包含下载后的数据.但是对于.rtf文件,我收到文档服务器正忙,请稍候片刻"消息,而不是实际内容.

假设如果我复制将URL粘贴到浏览器中,则.rtf文件中包含数据..
请帮我



in the above example .doc files contains data after downloading. but for .rtf files I am getting "Document server is busy please try after some time" message rather than the actual content.

suppose if i copy paste the url in browser .rtf file contains data..
Please help me guys

推荐答案

static public byte[] GetBytesFromUrl(string url)
    {
        byte[] b;
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
        WebResponse myResp = myReq.GetResponse();

        Stream stream = myResp.GetResponseStream();
        //int i;
        using (BinaryReader br = new BinaryReader(stream))
        {
            //i = (int)(stream.Length);
            b = br.ReadBytes(500000);
            br.Close();
        }
        myResp.Close();
        return b;
    }

    static public void WriteBytesToFile(string fileName, byte[] content)
    {
        FileStream fs = new FileStream(fileName, FileMode.Create);
        BinaryWriter w = new BinaryWriter(fs);
        try
        {
            w.Write(content);
        }
        finally
        {
            fs.Close();
            w.Close();
        }
    }





private void getDocument(string url )
   {
       
       string DestinationPath = Server.MapPath("targetdocumentspath");
       string filename = url.Substring(url.LastIndexOf('/') + 1);
       byte[] bytes = GetBytesFromUrl(url);
       WriteBytesToFile(DestinationPath + "/" + filename, bytes);       

   }


这篇关于有关使用HttpWebRequest下载文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆