Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WebBrowserControl , +
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 11-Apr-12 2:37am
hitech_s6.5K
Edited 1-Sep-12 19:23pm
v9
Comments
ryanb31 at 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_ssc at 12-Apr-12 1:27am
   
there is no session time out still am logged in
hitech_s at 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.
mark merrens at 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 at 28-Aug-12 17:03pm
   
Read over and over again, but I don't understand your question. Can we see some code here?
Dasaradhi_r at 29-Aug-12 3:02am
   
The question is quite fine for me.
If it is opening up in browser, ideally it should work with web browser control in windows application also. Please let me know the below details:
1) What is the size and other details of the document for which you are having issues with download?
2) In your web application is there any config setting related to this?
3) Are there are any specific settings you set for browser control in your win app?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);       
 
   }
  Permalink  
v2
Comments
Dasaradhi_r at 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)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 19 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100