Dailycode.info

Short solution for short problems

Write HTML content to file

Not so very hard. The only tricky part is when there are IFrames on the page.

string
html;
using (WebClient client = new WebClient())
{
    client.UseDefaultCredentials = true;
    html = client.DownloadString("http://dailycode.info/dailycardgames/#/home");
    using (FileStream fs = new FileStream(@"d:\temp\files\test" + DateTime.Now.Ticks.ToString() + ".htm", FileMode.OpenOrCreate))
    {
        using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
        {
            w.WriteLine(html);
        }
    }
}

Then I noticed that in my page (not the one from the example above) there were iframes. And they were not loaded. 

So I had to also download the iframes, save them to a file and replace the url of the iframe to the url of the file.

Also I noticed that for example the umlaut รถ was not shown correctly, so I had to include the UTF8 encoding in the download as well as in the file writer:

At the end it looked like this:

string html;
using (WebClient client = new WebClient())
{
    client.UseDefaultCredentials = true;
    client.Encoding = Encoding.UTF8;
    html = client.DownloadString("http://dailycode.info/dailycardgames/#/home");
 
    MatchCollection iframes = Regex.Matches(html, "<iframe.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase);
           
    int i = 0;
    foreach (Match m in iframes)
    {
        string url = m.Groups[1].Value;
        string iframe = client.DownloadString("http://dailycode.info/dailycardgames/" + url);
        iframe = iframe.Replace(@"../../images", @"D:\temp\images");
        var iframehtml = @"d:\temp\files\testiframe_"+i+"_" + DateTime.Now.Ticks.ToString() + ".htm";
        using (FileStream fs = new FileStream(iframehtml, FileMode.OpenOrCreate))
        {
            using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
            {
                w.WriteLine(iframe);
            }
        }
 
        html = html.Replace(url, iframehtml);
        i++;
    }
}
           
html = html.Replace(@"../images", @"D:\temp\images");
           
using (FileStream fs = new FileStream(@"d:\temp\files\test" + DateTime.Now.Ticks.ToString() + ".htm", FileMode.OpenOrCreate))
{
    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
    {
        w.WriteLine(html);
    }
}

As you can see I also replace the images location to a offline location. 



Logging: log4net type not resolved error!!!

I got this error when using log4net. Type is not resolved for member 'log4net.Util.PropertiesDictionary,log4net

It only occurred on the development machines. So we were looking for iisexpress problems. Could find a lot of unittest fixes etc, but this was not our problem. It simply crashed whenever we called logging in our web site. the dll's were nicely referenced.

When we used local iss, it works fine.

So at the end I found out that in iisexpress the dll was not always loaded because of multi threading. So the fix was to install the dll into the GAC:

gacutil /i D:\dev\packages\log4net\log4net.dll



Web API call Json object is null

I have a WCF service that call's a Web API 2 service. I serialize the object ot JSON and pass it to the API. But when deployed to the server the incoming object was null. I had a very hard time figuring out what was the problem, the server only teturned this message:

The remote server returned an error: (500) Internal Server Error

The code to call the web API was something like this:

using (var client = new WebClient())

{

    //var employeeJson = new JavaScriptSerializer().Serialize(empBDO);

    var empJson = JsonConvert.SerializeObject(empBDO);

 

    client.Headers[HttpRequestHeader.ContentType] = "application/json";

    empJson.LogMessage(CurrentProject, LoggingMode.Debug, "JSON to send");

 

    try

    {

        var response = client.UploadString(apiUrl, empJson);

    }

    catch (Exception apiex)

    {

        ("PostToAPI.CallAPI error = " + apiex.Message).LogMessage(CurrentProject, LoggingMode.Debug, apiex.StackTrace);

    }

}

If I called the API from an angular app with the same json that was logged, there was no problem. Everything worked. Then I started to add more and more logging and noticed that the incoming object in the API was null if I called it from the server.

At the end I had to add this line of code to the webclient that was calling the API:

    client.Encoding = UTF8Encoding.UTF8;

So the code looked like this:

using (var client = new WebClient())

{

    var empJson = JsonConvert.SerializeObject(empBDO);

    client.Headers[HttpRequestHeader.ContentType] = "application/json";

    client.Encoding = UTF8Encoding.UTF8;

    try

    {

        var response = client.UploadString(apiUrl, empJson);

    }

    catch (Exception apiex)

    {

        ("PostToAPI.CallAPI error = " + apiex.Message).LogMessage(CurrentProject, LoggingMode.Debug, apiex.StackTrace);

    }

}