我想创建一个程序,将帖子自动发送到我的网站 这是班级
using ershaco_panel.Enums;
using ershaco_panel.Interfaces;
using System;
using System.Threading;
using System.Windows.Forms;
namespace ershaco_panel.Classes
{
public class WebSite : IService
{
public WebSite(string username, string password)
{
Step = Steps.Login;
Username = username;
Password = password;
RunBrowserThread();
}
public WebBrowser Browser
{
get; set;
}
public string Password
{
get; set;
}
public Services ServiceType
{
get; set;
}
public Steps Step
{
get; set;
}
public string Username
{
get; set;
}
public void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if(Step == Steps.Login)
{
var browser = sender as WebBrowser;
var username = browser.Document.GetElementById("uid");
var password = browser.Document.GetElementById("pwd");
username.SetAttribute("Value", Username);
password.SetAttribute("Value", Password);
Console.WriteLine("Robot Navigated to {0} Form Count : {1}", e.Url, browser.Document.Forms.Count.ToString());
browser.Document.GetElementsByTagName("form")[0].InvokeMember("Submit");
Step = Steps.Dashboard;
}
}
public void RunBrowserThread()
{
var thread = new Thread(() => {
Browser = new WebBrowser();
Browser.DocumentCompleted += BrowserDocumentCompleted;
Browser.Navigate("http://www.myurl.com");
Application.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
public void SendPost(string content)
{
throw new NotImplementedException();
}
}
}
如您所见,我使用DocumentComplete管理WebBrowser项异步。但正如您所知,在C#中,WebBrowser对象运行activeX,因此它应该在具有STA
ApatmentState的另一个线程中实现。
我有一个名为' Step'显示浏览器当前步骤。现在在SendPost方法中我想更改Web浏览器URL并导航到它,我应该使用另一个线程吗?我得知如何实现SendPost方法。
感谢您的回复