I want to call a JavaScript function with HttpWebRequest or WebRequest in C#. I don't want to use a webbrowser which I can call invokemember.
Here is my code:
public void MyWebRequest(string url, string method, string data)
{
request = WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
// Set the Method property of the request to POST.
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
string postData = data;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
MyWebRequest("http://example.com", "POST", "javascript:onclick=\"try(1,3)\"");
try is a JS function which has two int parameters. I want to call the onclick method, but how can I pass parameters to the function.
onclick="try(1,3);"
WebRequest?WebBrowseris probably the best way to go. And it's only "slow" because it has to load all the plugins like JVM, JS Engine and any other abilities the page requires to operate.