0
我有一個網站,其中包含姓名,員工編號,工資,迄今爲止。基本上,我曾經打開該網站並手動輸入每個值。如何自動打開網站並執行操作
我想自動打開該網站,並使用C#將值賦予受尊重的項目。淨。我不知道這是否可能。任何人都可以請建議我的方法或替代過程?
我有一個網站,其中包含姓名,員工編號,工資,迄今爲止。基本上,我曾經打開該網站並手動輸入每個值。如何自動打開網站並執行操作
我想自動打開該網站,並使用C#將值賦予受尊重的項目。淨。我不知道這是否可能。任何人都可以請建議我的方法或替代過程?
您可以使用System.Net
命名空間的WebClient
以編程方式執行URL。 url可以包含查詢字符串,可以在加載url期間提取查詢字符串。然後可以將響應解碼爲字符串,該字符串本質上將是一個html,可以根據您的方便使用它。
以下是我必須執行此功能的方法。
public static string ReadUrlToString(string url, string method, string postParams, string userName, string password, string domain)
{
var wc = new WebClient();
// set the user agent to IE6
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)");
AssignCredentials(wc, userName, password, domain);
try
{
if (Is.EqualString(method, "POST"))
{
// Eg "field1=value1&field2=value2"
var bret = wc.UploadData(url, "POST", Encoding.UTF8.GetBytes(postParams));
return Encoding.UTF8.GetString(bret);
}
// actually execute the GET request
return wc.DownloadString(url);
}
catch (WebException we)
{
// WebException.Status holds useful information
//throw as needed
}
catch (Exception ex)
{
// other errors
//throw as needed
}
}
方法如下。
private static void AssignCredentials(WebClient wc, string userName, string password, string domain)
{
if (Is.NotEmptyString(userName))
{
wc.Credentials = Is.EmptyString(domain)
? new NetworkCredential(userName, password)
: new NetworkCredential(userName, password, domain);
}
}
希望這會有所幫助。
查看「網頁抓取」或「屏幕抓取」。 –
我沒有給你約翰,對不起。 – user3040784
您可以使用HttpClient發送HTTP請求並接收響應。我建議使用您的Web瀏覽器的調試器來確定發送了什麼請求和響應,並與您的客戶端進行模擬。 –