public class WPWorkerTests
{
public CookieContainer cookieJar = new CookieContainer();
public WPWorkerTests()
{
}
public void StartAdmin(object obj)
{
CookieContainer cJar = new CookieContainer();
string primer = getRequest("url to wordprss login", 0);
string respAdmin = postRequest(wpaObject.url, "log=admin&pwd=adminpw&wp-submit=Log+In&testcookie=1&redirect_to=http%3A%2F%2Fwordpress domain%2Fwp-admin%2F", 0);
string ha = getRequest("url to wordpress admin", 0);
}
public string getRequest(string url, int tries)
{
try
{
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWRequest.CookieContainer = cookieJar;
HttpWRequest.UserAgent = "IE 9 Experimental";
HttpWRequest.KeepAlive = true; //this is the default
HttpWRequest.Timeout = 10000;
HttpWRequest.Method = "GET";
HttpWRequest.ServicePoint.ConnectionLimit = 100;
HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), Encoding.ASCII);
string response = sr.ReadToEnd().ToLower();
sr.Close();
return response.ToLower();
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + url);
if (ex.ToString().Contains("404") == true)
{
return "404";
}
else if (ex.ToString().Contains("500") == true)
{
return "";
}
if (tries > 1)
{
Console.WriteLine();
Console.WriteLine(url);
Console.WriteLine();
Console.WriteLine(ex.ToString());
}
else
{
tries++;
System.Threading.Thread.Sleep(500);
return getRequest(url, tries);
}
}
return "";
}
public string postRequest(string url, string postdata, int tries)
{
try
{
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWRequest.CookieContainer = cookieJar;
HttpWRequest.UserAgent = "IE 9 Experimental";
HttpWRequest.KeepAlive = true; //this is the default
HttpWRequest.Referer = url;
HttpWRequest.Timeout = 120000;
HttpWRequest.Method = "POST";
HttpWRequest.Referer = url;
// add the content type so we can handle form data
HttpWRequest.ContentType = "application/x-www-form-urlencoded";
// we need to store the data into a byte array
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(postdata);
HttpWRequest.ContentLength = PostData.Length;
Stream tempStream = HttpWRequest.GetRequestStream();
// write the data to be posted to the Request Stream
tempStream.Write(PostData, 0, PostData.Length);
tempStream.Close();
HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), Encoding.ASCII);
string response = sr.ReadToEnd().ToLower();
sr.Close();
return response.ToLower();
}
catch (Exception ex)
{
if (tries > 1)
{
Console.WriteLine(ex.ToString());
}
else
{
tries++;
System.Threading.Thread.Sleep(500);
return postRequest(url, postdata, tries);
}
}
return "";
}
}