How to Download the File using HttpWebRequest and HttpWebResponse class(Cookies,Credentials,etc.)

后端 未结 2 1697
猫巷女王i
猫巷女王i 2020-12-03 07:46

Thanks icktoofay, I tried using HttpWebRequest and HttpWebResponse.

When I request for URL by passing the Credentials like UserName And P

相关标签:
2条回答
  • 2020-12-03 08:09

    This is how I do it:

    const string baseurl = "http://www.some......thing.com/";
    CookieContainer cookie;
    

    The first method logins to web server and gets session id:

    public Method1(string user, string password) {
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);
    
      req.Method = "POST";
      req.ContentType = "application/x-www-form-urlencoded";
      string login = string.Format("go=&Fuser={0}&Fpass={1}", user, password);
      byte[] postbuf = Encoding.ASCII.GetBytes(login);
      req.ContentLength = postbuf.Length;
      Stream rs = req.GetRequestStream();
      rs.Write(postbuf,0,postbuf.Length);
      rs.Close();
    
      cookie = req.CookieContainer = new CookieContainer();
    
      WebResponse resp = req.GetResponse();
      resp.Close();
    }
    

    The other method gets the file from server:

    string GetPage(string path) {
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(path);
      req.CookieContainer = cookie;
      WebResponse resp = req.GetResponse();
      string t = new StreamReader(resp.GetResponseStream(), Encoding.Default).ReadToEnd();
      return IsoToWin1250(t);
    }
    

    Note that I return the page as string. You would probably better return it as bytes[] to save to disk. If your jpeg files are small (they usually are not gigabyte in size), you can simply put them to memory stream, and then save to disk. It will take some 2 or 3 simple lines in C#, and not 30 lines of tough code with potential dangerous memory leaks like you provided above.

    0 讨论(0)
  • 2020-12-03 08:09
    public override DownloadItems GetItemStream(string itemID, object config = null, string downloadURL = null, string filePath = null, string)
        {
            DownloadItems downloadItems = new DownloadItems();
            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    using (FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
                    {
                        if (!string.IsNullOrEmpty(downloadURL))
                        {
                            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(downloadURL);
                            request.Method = WebRequestMethods.Http.Get;
                            request.PreAuthenticate = true;
                            request.UseDefaultCredentials = true;
                            const int BUFFER_SIZE = 16 * 1024;
                            {
                                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                                {
                                    using (var responseStream = response.GetResponseStream())
                                    {
                                        var buffer = new byte[BUFFER_SIZE];
                                        int bytesRead;
                                        do
                                        {
                                            bytesRead = responseStream.Read(buffer, 0, BUFFER_SIZE);
                                            fileStream.Write(buffer, 0, bytesRead);
                                        } while (bytesRead > 0);
                                    }
                                }
                                fileStream.Close();
                                downloadItems.IsSuccess = true;
                            }
                        }
                        else
                            downloadItems.IsSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                downloadItems.IsSuccess = false;
                downloadItems.Exception = ex;
            }
            return downloadItems;
        }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题