I'm fixing a module checking Google Keyword Ranking, each keywords for each 30s. Those day, Google block the request, the module is very important for our project.
I'm using HtmlAgilityPack in my module, the code is something like that:
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.Method = "GET";
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6";
// read last cookie from disk, send it with request
CookieContainer cookies = ReadCookiesFromDisk(cookieFile);
req.CookieContainer = cookies;
// request google, with url like: https://www.google.com/search?start=0&num=20&q=may+do+huyet+ap&hl=vi
resp = req.GetResponse() as HttpWebResponse;
// save new cookie to disk
CookieContainer newCookies = req.CookieContainer;
WriteCookiesToDisk(cookieFile, newCookies); // write cookie to disk using Serialize Object to BinaryFormatter
And as below is 2 function read/write cookie to binary file.
public void WriteCookiesToDisk(string file, CookieContainer cookieJar)
{
using (Stream stream = File.Create(file))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, cookieJar);
}
}
public CookieContainer ReadCookiesFromDisk(string file)
{
try
{
using (Stream stream = File.Open(file, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
return (CookieContainer)formatter.Deserialize(stream);
}
}
catch (Exception e)
{
return new CookieContainer();
}
}
After 1 day, Google block the request by captcha, and I found that:
If I change my IP, I can continue request. So, Google block by IP.
Using Opera with Enable cookie, Opera still search Google even when Google block my module with the same keywords, but if I disable cookie of Opera, Opera can not search Google like my module.
So, I'am trying to enable cookie like Opera (as my code on above), but Opera work, but my code does not work.
Could you please help me?