-2

I want download file http://img1.ak.crunchyroll.com/i/croll_manga/e/257692e8c297b8907e2607964454b941_1438752352_main to string.

In C++ I writed function

string opener(string url)
{
  CURL *curl;
  CURLcode res;
  std::string readBuffer;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
  return readBuffer;
}

This work properly.

In C# I writed this class

namespace Crmrip
{
    class opener
    {
        public static string open(string url)
        {

            using (WebClient page = new WebClient())
            {
                try
                {
                    page.Encoding = Encoding.UTF8;
                    page.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
                    string result = page.DownloadString(url);
                    return result;
                }

                catch (WebException er)
                {
                    MessageBox.Show(er.ToString());
                    return null;
                }
            }

        }
    }
}

This class work not properly. Downloaded file is too big. File downloaded in C++ have 487 778 bytes and is OK. File downloaded in C# have 879 910 and is too big.

Why file downloaded in C# is too big ?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 1
    Have you tried with `page.DownloadFile` instead of `page.DownloadData`? – NASSER Aug 21 '15 at 10:29
  • Is your code compiling? As I know `page.DownloadData` returns `byte[]` but you getting it into `string` – NASSER Aug 21 '15 at 10:33
  • Sorry I pasted wrong code. Properly should be page.DownloadString(url). page.DownloadData was from from my test downloading file as data. This is code of my class file that I writed for my aplication and yes code is code compiling to me. – Marcin Kurzawski Aug 21 '15 at 10:40
  • Image from URL I want save on disk but first I heve to decrypt him by XOR him by 0x42. – Marcin Kurzawski Aug 21 '15 at 11:28

2 Answers2

0

Use DownloadData to get the response as byte array. Do your XOR and write the result to a file

So changing your code a little bit:

using (WebClient page = new WebClient())
{
    try
    {
        byte[] result = page.DownloadData(url);
        for(int i=0;i<result.Length;i++)
        {
            result[i] ^= 0x42;
        }
        File.WriteAllBytes(filename, result);
    }

    catch (WebException er)
    {
        MessageBox.Show(er.ToString());

    }
}
Eser
  • 12,346
  • 1
  • 22
  • 32
  • @MarcinKurzawski If that answer helped, you may think to accept it See http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Eser Aug 28 '15 at 13:30
0

Seems to work:

void Main()
{
    string url = "http://img1.ak.crunchyroll.com/i/croll_manga/e/257692e8c297b8907e2607964454b941_1438752352_main";
    Console.WriteLine(open(url));
}


 public static async Task<string> open(string url)
    {

        using (HttpClient page = new HttpClient())
        {
            try
            {
                //page.Encoding = Encoding.UTF8;
                //page.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
                page.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0");
                return await page.GetStringAsync(url);
            }

            catch (Exception er)
            {
                //MessageBox.Show(er.ToString());
                return null;
            }
        }

    }

I'm not sure whether encoding will be an issue - here's a reference

Community
  • 1
  • 1
jacoblambert
  • 787
  • 1
  • 11
  • 18