0

I want to run a script using API calls in C#. I don't want the webpage to open and just the script should run. I am trying this:

HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

HtmlDocument doc; //I have tried HtmlDocument = new HtmlDocument();, didn't work.
var resultStream = response.GetResponseStream();
doc.LoadHtml(resultStream); // I have tried using Load instead of LoadHtml,didn't work out.
doc.InvokeScript("Submit");

I get an error, use of unassigned variable doc. and doc doesn't contain function name LoadHtml. I have adding the Microsoft.VisualStudio.TestTools.UITesting.HtmlControls; , didn't help.

I have checked th questions HtmlDocument.LoadHtml from WebResponse? and Get HTML code from website in C# but they didn't get an error on doc.

Any solutions.

Community
  • 1
  • 1

2 Answers2

1

You will have to change the way you load the HtmlDocument

string html = new WebClient().DownloadString(URL);
WebBrowser browser = new WebBrowser()
{
    ScriptErrorsSuppressed = true,
    DocumentText = string.Empty
};
HtmlDocument doc = browser.Document.OpenNew(true);
doc.Write(html);
doc.InvokeScript("Submit");

Hope it works.

Paulo Junior
  • 266
  • 2
  • 8
0
HtmlDocument doc; //I have tried HtmlDocument = new HtmlDocument();, didn't work.

This won't work unless you do:

HtmlDocument doc = new HtmlDocument(/*someUri*/, /*documentLocation*/);

You need to initialize doc. This is why you are seeing

use of unassigned variable doc

Check out the documentation here for details:

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.webtesting.htmldocument.aspx

According to it, the constructor signature is:

HtmlDocument(Uri, String)

with the description:

Initializes a new instance of the HtmlDocument class. This constructor takes a >string and uses it as the document.

Also, by looking at the documentation, it doesn't have a method LoadHtml()

Dudemanword
  • 710
  • 1
  • 6
  • 20