Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
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;
                    }
                }


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
Posted
Updated 1-Sep-12 18:23pm
v9
Comments
ZurdoDev 11-Apr-12 10:28am    
Sounds like your session is timing out. Also, unless someone else who reads this has had the exact same problem I doubt anyone can help unless you share some code and more details.
hitech_s 12-Apr-12 1:27am    
there is no session time out still am logged in
hitech_s 28-Aug-12 14:18pm    
may i know the guys who down voted and why ?
you should give proper reason to down vote something . please let me know...don't be think in negative way and don't make people sad.
R. Giskard Reventlov 28-Aug-12 15:45pm    
Saying don't make people sad will get you down-voted around here.

You've probably been down-voted because you have asked a question without providing sufficient information. None of us (that I'm aware of) are telepathic so there is no way we can know what is going on without some code.
Christian Amado 28-Aug-12 17:03pm    
Read over and over again, but I don't understand your question. Can we see some code here?

1 solution

C#
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();
        }
    }


XML
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);       

   }
 
Share this answer
 
v2
Comments
Dasaradhi_r 29-Aug-12 13:57pm    
Your code snippet requires, Url to be provided.
Just curious to know, how Url of link can be picked up from a web browser control?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900