2

I've got a basic "wrapper" WinForms app which has a few basic controls and a WebBrowser control (System.Windows.Forms.WebBrowser). This links to a web service which does all the actual "work" of my application.

A requirement has arisen to pass some basic data between the applications. Some of this can be achieved using the DocumentCompleted/Navigated events and using the URL property to see what page was loaded, and redirect the request elsewhere.

However, I'm struggling to work out how I can pass data that is not easily encompassed in the URL.

Is there anyway to set cookies in the request, or at least access cookies from the response?

3
  • In what direction do you want to pass the data? App to page, or page to app -- can you give an example? And, do you have control over the contents of the page?
    – felix-b
    Jan 31, 2018 at 16:07
  • I'm not proficient with WebBrowser control, but I think it should be possible to get the cookies on Navigated event using webBrowser.Document.Cookie property. Jan 31, 2018 at 16:44
  • It's not clear what you are going to do, but you may find this post useful: Interaction between WebBrowser Control and Windows Forms Jan 31, 2018 at 19:16

1 Answer 1

5

You can pass and get data by calling methods from passed object as ObjectForScripting. Try this-

[ComVisible(true)]
public class MyScriptingClass{
    private string SomeData;
    public string GetSomeData(){
       return SomeData + " Something";
    }
    public void SetSomeData(string some){
       SomeData = some;
    }
}

And set ObjectForScripting property of your webBrowser control -

webBrowser.ObjectForScripting = new MyScriptingClass();

Now, in your javascript code call the methods like this -

var someVarFromJs = window.external.GetSomeData();
window.external.SetSomeData("Something to set");

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.