The underlying connection was closed - Having fun posting code samples

The underlying connection was closed. ErrorI had a bit of a nightmare getting the previous post online. No matter what I did I was getting this error message "The underlying connection was closed: An unexpected error occurred on a receive".

I had just installed beta 2 of Windows Live Writer, so I uninstalled that and went back to beta 1, but that still had the same issue. I then logged into the Web UI for my blog and pasted the contents of my post in their and got more errors. At this point I started to think that there was something wrong with geekswithblogs, luckily the first public CTP of Katmai (SQL Server 2008) had just been released, so I did this post which worked first time.

So, now I was left with the conclusion that there was something in the post that was causing difficulties. As there was a code sample with XML/A in there I figured that was the most likely culprit and sure enough, as soon as I posted a draft copy without the code sample it worked fine. So the next thing I tried was copying the code out to notepad and back in, which stripped out all the nice color coding that the "Insert from Visual Studio" plugin for Live Writer had added. Still no luck.

Next I resorted to looking through the underlying html. But I could not see anything in there that looked like it would be causing any issues. The angle brackets in the XML/A command that I was building all appeared to be escaped correctly, but I could not remember if I had posted any samples with XML since the blogging engine had been upgraded from .Text to SubText, so I tried posting a draft excluding the XML/A, but that still did not work.

Getting desperate I then tried removing the error handling - I did not really expect that this would make any difference at all, but it was not really material to the sample anyway (and who puts error handling in code samples anyway?). As I expected this had no effect. Failing to logically (and illogically in the case of the error handling) identify the cause of the issue I was now down to the binary search. I chopped out the last half of the code sample and the draft posted successfully, so the issue was in the last half. I added more code until I eventually got down to the following line


That was it, that one line was causing the issue. My only guess at this point is that something was assuming that this was an attempt at some sort of injection attack, either looking for the. If you look at the source of the above line what you will see is the following.


I have broken up the text with a span tag (which is a sort of inline placeholder container) so that whatever was blocking the post now does not see any suspicious text. At first I thought it was the ExecuteNonQuery() function that it was picking up on, but it was actually the text "cmd.exe" that is causing the issue.

This one had me banging my head against a brick wall for quite a while so I hope posting this here might save someone else some time that could otherwise be spent in more productive pursuits.

Print | posted on Wednesday, June 6, 2007 9:55 PM