0

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);"
11
  • sorry, I edited my code !! I forgot to add some code.. now its ok Commented Apr 25, 2012 at 13:36
  • The javascript function resides on the page you're fetching with WebRequest? Commented Apr 25, 2012 at 13:37
  • You are doing a HTTP POST request to a server. It happens you pass some JavaScript with the request. Do you now expect to server will "execute" that code? If no, then you will receive a response, which depends on the server. If by chance the server responds with the same JavaScript code, do you expect that the response, on your side, will "execute" the code? Just trying to understand the question... Commented Apr 25, 2012 at 13:40
  • 1
    @HarunAbi: If you need the functionality of the site, yes, the WebBrowser is 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. Commented Apr 25, 2012 at 13:49
  • 1
    really thank you Brad, its so clear.. Okay I'll use a webbrowser.. :) Commented Apr 25, 2012 at 13:50

1 Answer 1

3

Just so this isn't an open-ended question...

WebRequest is essentially a "glorified" socket library that establishes a connection to and from a web server. It's not more than a means to transfer data between a client (the app using the WebRequest) and the server hosting the site.

For the same reason if you viewed the dataStream (referencing your code) you'd only see HTML markup and not actual layout elements and colors, styles, etc., javasript also won't work. HTML needs a rendering agent, JavaScript needs an engine.

So, if you need the ability to use JavaScript included on the page, WebBrowser is your best bet. It will load up any libraries/plugins necessary to run any JavaScript found on the page. It may be slower, yes, but that's because it's giving you a lot more than just transferring data (it's also rendering HTML, executing any initialization scripts, and binding to any elements that the page has defined to be necessary for the aesthetics.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.