C# – Auto fill in form of website

Last week I discovered a web site where I can play Sudoku, solve it to participate in a redemption for a 5€ Amazon Coupon. To solve this Sudoku puzzle I can use my service http://hintdesk.com/Web/Silverlight/SudSol/ . I just take the puzzle, submit it to my service and let it solved, then fill in the result back to the puzzle. However I see it’s very uncomfortable for doing this job by hand. I must read carefully each square and enter it correctly to my service. I would like to do it automatically, that means I need a program which read the puzzle, solve it, submit result and submit my name and email. In this small blog, I will demonstrate a code snippet to do that. The Sudoku puzzle looks like the image below

Sudoku

To read the information, I use the control System.Windows.Controls.Webbrowser to load the website because I would like to write a WPF application. You can use System.Windows.Forms.Webbrowser if you are writing a Windows Form application.

 m_wb.Navigate("http://enteryoururlhere.com");
m_wb.LoadCompleted += new LoadCompletedEventHandler(m_wb_LoadCompleted);

At the handler of LoadCompleted event I will start to process data by reading HTML elements according to tags. The data of the puzzle will be save in a List

IHTMLElementCollection m_hec;
...
m_hec = (m_wb.Document as HTMLDocumentClass).getElementsByTagName("input");
List<string> lstInput = new List<string>();
foreach (HTMLInputElementClass he in m_hec)
{
	if (he.getAttribute("name").ToString() == "FIELD[]")
		if (he.getAttribute("value") != null)
			lstInput.Add(he.getAttribute("value").ToString());
		else
			lstInput.Add("");
}

To use HTMLDocumentClass you need a reference to Microsoft.mshtml

Microsoft.mshtml

Using this class, may be you’ll meet this error “Interop type ‘mshtml.HTMLDocumentClass’ cannot be embedded. Use the applicable interface instead.“. The error says that HTMLDocumentClass cannot be embedded – this is because only interface types can be embedded. To solve this error, right click on reference Microsoft.mshtml, in the Properties Window set “Embed Interop Type” to false

Embed Interop Type

The size of Microsoft.mshtml.dll is about 7.64 MB (8013664 bytes) and setting Embed Interop Type=false on this will cause the entire 8MB to be loaded into the process. With Embed Interop Type=true compilers will only embed those interfaces from Microsoft.mshtml.dll that are used by your application. So, total working write and read disk size will be significantly smaller. However cause of this error, we need to load complete DLL into process.
The data will be then parsed according to a predefined format and transfered to Sudoku Solver to solve it and receive result back.

string strQuiz = "";
for (int nRow = 0; nRow < lstInput.Count; nRow++)
{
	if (lstInput[nRow] != "")
		strQuiz += lstInput[nRow];
	else
		strQuiz += ".";
}

Dictionary<string, string> dictResult = SolMachine.search(SolMachine.parse_grid(strQuiz));

This result needs to be written back to puzzle with following code

if (dictResult != null)
{
	string[] strDigit = new string[dictResult.Count];
	dictResult.Values.CopyTo(strDigit, 0);
	int nCount = 0;
	foreach (HTMLInputElementClass he in m_hec)
	{
		if (he.getAttribute("name").ToString() == "FIELD[]" && he.getAttribute("value") == null)
		{
			he.setAttribute("value", strDigit[nCount]);
			nCount++;
		}
		else if (he.getAttribute("name").ToString() == "FIELD[]" && he.getAttribute("value") != null)
		{
			nCount++;
		}
	}
}
else
{
	System.Windows.MessageBox.Show("Sorry, this Sudoku puzzle can not be solved", "hintdesk.com", MessageBoxButton.OK);
}

and then click the submit button

foreach (HTMLInputElementClass he in m_hec)
{
	if (he.getAttribute("name").ToString() == "CHECK")
	{
		he.IHTMLElement_click();
		break;
	}
}

That’s all and you’ll have an intelligent software to help you solving Sudoku, submit result and win a prize if you are lucky.

4 thoughts on “C# – Auto fill in form of website”

  1. Không biết lão kiếm đâu ra cái vụ Coupon hay thế :))

    Về ý tưởng và các thực hiện của lão thì ko có gì để bàn, nó quá tốt rùi. Tui chỉ góp ý thêm 2 cách nữa cho nó lẹ hơn:

    1. Dùng ngay cái WebBrowser của .Net lun, đỡ cái MS.html kia, sau đó thực hiện như lão.

    2. Dùng HTTPWebRequest, sau đó dùng Regular Expression để parse ra, chắc đây là cách nhanh nhất 😀

  2. @Lion King: Thắng 2 lần rồi lão ah. Được 10 Euro trên Amazon rồi. Để dành tiền mua … Ipad. =)). Còn vấn đề là nếu dùng WebBrowser hay HTTPWebRequest thì đã quá quen thuộc nên lão rồng ko dùng tới. Muốn xử lý ngay trên HTML Object cho mới lạ.
    Thân.

Leave a Reply

Your email address will not be published. Required fields are marked *