Search code examples
c#.nethttprequestwatin

How to save dynamic image in WatIn


I am trying to save an image that changes dynamically with each request.

I tried WatIn and HttpWebRequest (getting new image)

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.test.com");
request.AllowAutoRedirect = false;
WebResponse response = request.GetResponse();

using (Stream stream = response.GetResponseStream())
using (FileStream fs = File.OpenWrite(ImageCodePath))
{
    byte[] bytes = new byte[1024];
    int count;

    while ((count = stream.Read(bytes, 0, bytes.Length)) != 0)
    {
        fs.Write(bytes, 0, count);
    }
}

and (User32.dll) URLDownloadToFile (getting new image)

[DllImport("urlmon.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern Int32 URLDownloadToFile(Int32 pCaller, string szURL, string szFileName, Int32 dwReserved, Int32 lpfnCB);
 URLDownloadToFile(0, "https://test.reporterImages.php?MAIN_THEME=1", ImageCodePath, 0, 0);

I looked in all the temp folders and still can't find the image.

Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache),"Content.IE5");

Every time I try to save it the server builds a new image and returns it. If I right click on the image and choose:

Save picture as...

It will save the picture. I need to somehow implement this method (Right-click, Save picture as...) in WatIn in IE, or somehow download it with HttpRequest from my HTML page without server interaction.

Does anyone know how I can do this?


Solution

  • As i understand, the idea is to capture current CAPTCHA image on page in browser to bypass it by some text recognition (btw, strange idea). And no problem to get image url (it is always the same). In this case you can use API to access browser cache. Specifically for IE FindFirstUrlCacheEntry/FindNextUrlCacheEntry It can help if your application hosts WebBrowser.