Search code examples
c#imagehttpwebrequesthttprequest

Disable image download for HttpWebRequest


Is it possible to say a webrequest to only get text-based data from a site? And if it is how should I do this?

The only thing I can imagine is to search in the response string and remove all the image-tags. But this is a very bad way to do this...

EDIT: this is my code snippet:

            string baseUrl = kvPair.Value[0];
            string loginUrl = kvPair.Value[1];
            string notifyUrl = kvPair.Value[2];
            cc = new CookieContainer();
            string loginDetails = DataCollector.GetLoginDetails(baseUrl, ref cc);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginUrl);
            request.Method = "POST";
            request.Accept = "text/*";
            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            request.CookieContainer = cc;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36";
            Byte[] data = Encoding.ASCII.GetBytes(loginDetails);
            request.ContentLength = data.Length;
            using (Stream s = request.GetRequestStream())
            {
                s.Write(data, 0, data.Length);
            }
            HttpWebResponse res = (HttpWebResponse)request.GetResponse();
            request = (HttpWebRequest)WebRequest.Create(notifyUrl);
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36";
            request.CookieContainer = cc;
            res = (HttpWebResponse)request.GetResponse();
            Stream streamResponse = res.GetResponseStream();
            using (StreamReader sr = new StreamReader(streamResponse))
            {
                ViewData["data"] += "<div style=\"float: left; margin-bottom: 50px;\">" + sr.ReadToEnd() + "</div>";
            }

Solution

  • I found myself a good coding solution:

    public static string StripImages(string input)
    {
       return Regex.Replace(input, "<img.*?>", String.Empty);
    }
    

    this kills all images but only as soon as you have loaded all the images so there is no savings in transfered data in this solution...