[英]C# function execute javascript function in web page using webclient
我需要一些代碼方面的幫助。 我有以下代碼,它非常有效地允許我以編程方式登錄到供應商的網站,然后從中為我們的數據庫抓取圖像。 這很好用。 問題是我還需要獲取最新數據 - 這是頁面上的導出按鈕(導出到 excel)。 使用 F12 和提琴手我可以看到它調用了 java 腳本 function。 我的知識在網上有點有限 - 我讀到客戶端可能不是 go 調用 javascript 但 webbrowser 的方式? (我是一名集成專家,所以 SQL、SSIS、邏輯應用程序或 c# 中的基本編碼更適合我)。 If I need to change to web browser how do I persist cookies from login etc. I'm coding this in azure function http trigger that will be used in logic apps flow.
public class WebClientEx : WebClient
{
public CookieContainer CookieContainer { get; private set; }
public WebClientEx()
{
CookieContainer = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = CookieContainer;
}
return request;
}
}
static string GetSourceForMyShowsPage(string JobId)
{
var sqlinsert = new SQLInsert();
using (var client = new WebClientEx())
{
var values = new NameValueCollection
{
{ "UserName", "sss" },
{ "Password", "sss" },
};
// Authenticate
client.UploadValues("url/Login", values);
//get the image url that we need to get the image in bytes - we might need to change this to a byte method?
string responseMessage = client.DownloadString("myurl" + JobId);
string imageurl = "";
byte[] imageBytes;
//search for images in string
HtmlDocument document = new HtmlDocument();
document.LoadHtml(responseMessage);
document.DocumentNode.Descendants("img")
.Where(e =>
{
string src = e.GetAttributeValue("src", null) ?? "";
return !string.IsNullOrEmpty(src) && src.StartsWith(@"/Photo");
})
.ToList()
.ForEach(x =>
{
imageurl = x.GetAttributeValue("src", null);
imageurl = imageurl.Substring(0, imageurl.IndexOf("width") - 1);
imageurl = imageurl.Replace("amp;", "");
imageurl = imageurl.Replace("&", "");
imageurl = "url/" + imageurl;
imageBytes = client.DownloadData(imageurl);
string base64image = Convert.ToBase64String(imageBytes);
//upload data to sql table
sqlinsert.executeInsert(JobId, base64image);
});
// Download desired page
return client.DownloadString("url" + JobId);
}
}
javascript function 被稱為:
function onExportClicked() {
var data = ST.Util.serialiseForm($('#shared-tag-search-form'));
var url = '/Reports/SharedTag';
var form = $('<form method="POST" action="' + url + '">');
data.push({ name: 'type', value: 'ExportToXls' });
$.each(data, function (k, v) {
form.append($('<input type="hidden" name="' + v.name + '" value="' + v.value + '">'));
});
$('body').append(form);
form.submit();
}
感謝分散。 我正在使用客戶端的上傳值並調用 api,然后使用寫入所有字節獲取數據。
client.BaseAddress = "url/";
var url = "Reports/SharedTag";
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36");
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
var formdata = new NameValueCollection
{
{ "Search.Reported.StartDate", "27/07/2022" },
{ "type", "ExportToXls" },
};
byte[] result = client.UploadValues(url, "POST", formdata);
File.WriteAllBytes(@"C:\Temp\Excel.xls", result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.