CefSharp make a POST web api call Win Forms c#












0















I want to use CefSharp to make a POST web api call in Win Forms c#.



I have Basic Authentication for my POST request. But if I run the code I get the error on line IFrame frame = browser.GetMainFrame();:




Browser is not yet initialized. Use the IsBrowserInitializedChanged
event and check the IsBrowserInitialized property to determine when
the browser has been intialized.




Is there a way to resolve the same?



Following is my code:



public partial class Form1 : Form
{

ChromiumWebBrowser browser = null;
public Form1()
{
InitializeComponent();

Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("http://ctstest.azurewebsites.net/api/default");
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;

PostTest.Navigate(browser, "http://ctstest.azurewebsites.net/api/default", null, "application/json");

}
}

public static class PostTest
{
public static void Navigate(this IWebBrowser browser, string url, byte postDataBytes, string contentType)
{
IFrame frame = browser.GetMainFrame();
IRequest request = frame.CreateRequest();

request.Url = url;
request.Method = "POST";

request.InitializePostData();
var element = request.PostData.CreatePostDataElement();
element.Bytes = postDataBytes;
request.PostData.AddElement(element);

NameValueCollection headers = new NameValueCollection();
headers.Add("Content-Type", contentType);
request.Headers = headers;

frame.LoadRequest(request);

frame.GetTextAsync().ContinueWith(taskHtml =>
{
var html = taskHtml.Result;
System.Windows.Forms.MessageBox.Show(html);
});

string script = string.Format("document.documentElement.outerHTML;");
frame.EvaluateScriptAsync(script).ContinueWith(x =>
{
var response = x.Result;

if (response.Success && response.Result != null)
{
var fullhtml = response.Result;
System.Windows.Forms.MessageBox.Show(fullhtml.ToString());
}
});
}

}
}


Thanks.










share|improve this question

























  • Subscribe to the IsBrowserInitializedChanged event and check cefsharp.github.io/api/67.0.0/html/…

    – amaitland
    Nov 21 '18 at 9:20











  • Your Navigate method never gives the web page time to actually load before attempting the call to GetTextAsync, similar problem with your use of EvaluateScriptAsync. You should read github.com/cefsharp/CefSharp/wiki/…

    – amaitland
    Nov 21 '18 at 9:22
















0















I want to use CefSharp to make a POST web api call in Win Forms c#.



I have Basic Authentication for my POST request. But if I run the code I get the error on line IFrame frame = browser.GetMainFrame();:




Browser is not yet initialized. Use the IsBrowserInitializedChanged
event and check the IsBrowserInitialized property to determine when
the browser has been intialized.




Is there a way to resolve the same?



Following is my code:



public partial class Form1 : Form
{

ChromiumWebBrowser browser = null;
public Form1()
{
InitializeComponent();

Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("http://ctstest.azurewebsites.net/api/default");
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;

PostTest.Navigate(browser, "http://ctstest.azurewebsites.net/api/default", null, "application/json");

}
}

public static class PostTest
{
public static void Navigate(this IWebBrowser browser, string url, byte postDataBytes, string contentType)
{
IFrame frame = browser.GetMainFrame();
IRequest request = frame.CreateRequest();

request.Url = url;
request.Method = "POST";

request.InitializePostData();
var element = request.PostData.CreatePostDataElement();
element.Bytes = postDataBytes;
request.PostData.AddElement(element);

NameValueCollection headers = new NameValueCollection();
headers.Add("Content-Type", contentType);
request.Headers = headers;

frame.LoadRequest(request);

frame.GetTextAsync().ContinueWith(taskHtml =>
{
var html = taskHtml.Result;
System.Windows.Forms.MessageBox.Show(html);
});

string script = string.Format("document.documentElement.outerHTML;");
frame.EvaluateScriptAsync(script).ContinueWith(x =>
{
var response = x.Result;

if (response.Success && response.Result != null)
{
var fullhtml = response.Result;
System.Windows.Forms.MessageBox.Show(fullhtml.ToString());
}
});
}

}
}


Thanks.










share|improve this question

























  • Subscribe to the IsBrowserInitializedChanged event and check cefsharp.github.io/api/67.0.0/html/…

    – amaitland
    Nov 21 '18 at 9:20











  • Your Navigate method never gives the web page time to actually load before attempting the call to GetTextAsync, similar problem with your use of EvaluateScriptAsync. You should read github.com/cefsharp/CefSharp/wiki/…

    – amaitland
    Nov 21 '18 at 9:22














0












0








0








I want to use CefSharp to make a POST web api call in Win Forms c#.



I have Basic Authentication for my POST request. But if I run the code I get the error on line IFrame frame = browser.GetMainFrame();:




Browser is not yet initialized. Use the IsBrowserInitializedChanged
event and check the IsBrowserInitialized property to determine when
the browser has been intialized.




Is there a way to resolve the same?



Following is my code:



public partial class Form1 : Form
{

ChromiumWebBrowser browser = null;
public Form1()
{
InitializeComponent();

Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("http://ctstest.azurewebsites.net/api/default");
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;

PostTest.Navigate(browser, "http://ctstest.azurewebsites.net/api/default", null, "application/json");

}
}

public static class PostTest
{
public static void Navigate(this IWebBrowser browser, string url, byte postDataBytes, string contentType)
{
IFrame frame = browser.GetMainFrame();
IRequest request = frame.CreateRequest();

request.Url = url;
request.Method = "POST";

request.InitializePostData();
var element = request.PostData.CreatePostDataElement();
element.Bytes = postDataBytes;
request.PostData.AddElement(element);

NameValueCollection headers = new NameValueCollection();
headers.Add("Content-Type", contentType);
request.Headers = headers;

frame.LoadRequest(request);

frame.GetTextAsync().ContinueWith(taskHtml =>
{
var html = taskHtml.Result;
System.Windows.Forms.MessageBox.Show(html);
});

string script = string.Format("document.documentElement.outerHTML;");
frame.EvaluateScriptAsync(script).ContinueWith(x =>
{
var response = x.Result;

if (response.Success && response.Result != null)
{
var fullhtml = response.Result;
System.Windows.Forms.MessageBox.Show(fullhtml.ToString());
}
});
}

}
}


Thanks.










share|improve this question
















I want to use CefSharp to make a POST web api call in Win Forms c#.



I have Basic Authentication for my POST request. But if I run the code I get the error on line IFrame frame = browser.GetMainFrame();:




Browser is not yet initialized. Use the IsBrowserInitializedChanged
event and check the IsBrowserInitialized property to determine when
the browser has been intialized.




Is there a way to resolve the same?



Following is my code:



public partial class Form1 : Form
{

ChromiumWebBrowser browser = null;
public Form1()
{
InitializeComponent();

Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("http://ctstest.azurewebsites.net/api/default");
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;

PostTest.Navigate(browser, "http://ctstest.azurewebsites.net/api/default", null, "application/json");

}
}

public static class PostTest
{
public static void Navigate(this IWebBrowser browser, string url, byte postDataBytes, string contentType)
{
IFrame frame = browser.GetMainFrame();
IRequest request = frame.CreateRequest();

request.Url = url;
request.Method = "POST";

request.InitializePostData();
var element = request.PostData.CreatePostDataElement();
element.Bytes = postDataBytes;
request.PostData.AddElement(element);

NameValueCollection headers = new NameValueCollection();
headers.Add("Content-Type", contentType);
request.Headers = headers;

frame.LoadRequest(request);

frame.GetTextAsync().ContinueWith(taskHtml =>
{
var html = taskHtml.Result;
System.Windows.Forms.MessageBox.Show(html);
});

string script = string.Format("document.documentElement.outerHTML;");
frame.EvaluateScriptAsync(script).ContinueWith(x =>
{
var response = x.Result;

if (response.Success && response.Result != null)
{
var fullhtml = response.Result;
System.Windows.Forms.MessageBox.Show(fullhtml.ToString());
}
});
}

}
}


Thanks.







c# winforms post cefsharp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 8:52









JohnB

1,89011218




1,89011218










asked Nov 21 '18 at 5:08









M. GomesM. Gomes

46




46













  • Subscribe to the IsBrowserInitializedChanged event and check cefsharp.github.io/api/67.0.0/html/…

    – amaitland
    Nov 21 '18 at 9:20











  • Your Navigate method never gives the web page time to actually load before attempting the call to GetTextAsync, similar problem with your use of EvaluateScriptAsync. You should read github.com/cefsharp/CefSharp/wiki/…

    – amaitland
    Nov 21 '18 at 9:22



















  • Subscribe to the IsBrowserInitializedChanged event and check cefsharp.github.io/api/67.0.0/html/…

    – amaitland
    Nov 21 '18 at 9:20











  • Your Navigate method never gives the web page time to actually load before attempting the call to GetTextAsync, similar problem with your use of EvaluateScriptAsync. You should read github.com/cefsharp/CefSharp/wiki/…

    – amaitland
    Nov 21 '18 at 9:22

















Subscribe to the IsBrowserInitializedChanged event and check cefsharp.github.io/api/67.0.0/html/…

– amaitland
Nov 21 '18 at 9:20





Subscribe to the IsBrowserInitializedChanged event and check cefsharp.github.io/api/67.0.0/html/…

– amaitland
Nov 21 '18 at 9:20













Your Navigate method never gives the web page time to actually load before attempting the call to GetTextAsync, similar problem with your use of EvaluateScriptAsync. You should read github.com/cefsharp/CefSharp/wiki/…

– amaitland
Nov 21 '18 at 9:22





Your Navigate method never gives the web page time to actually load before attempting the call to GetTextAsync, similar problem with your use of EvaluateScriptAsync. You should read github.com/cefsharp/CefSharp/wiki/…

– amaitland
Nov 21 '18 at 9:22












1 Answer
1






active

oldest

votes


















1














Well like the error message is telling you, you should check if the browser is already initialized.



I don't know the implementation of the class ChromiumWebBrowser but I would try to first navigate to the desired url



browser.navigate(yourUrl);


and maybe check if the browser has already navigated to your url by subscribing to the event IsBrowserInitialized before calling browser.GetMainFrame() (like amaitland mentioned)






share|improve this answer


























  • I'm downvoting this as I believe calling Thread.Sleep in a tight loop should be avoided at all costs.

    – amaitland
    Nov 21 '18 at 9:19











  • yup. As you mentioned in your answer subscribing to the event is definitely the better way to do it :-)

    – ChrizzleWhizzle
    Nov 21 '18 at 9:29











  • By all means edit your answer and I'll happily change my vote.

    – amaitland
    Nov 21 '18 at 9:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405575%2fcefsharp-make-a-post-web-api-call-win-forms-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Well like the error message is telling you, you should check if the browser is already initialized.



I don't know the implementation of the class ChromiumWebBrowser but I would try to first navigate to the desired url



browser.navigate(yourUrl);


and maybe check if the browser has already navigated to your url by subscribing to the event IsBrowserInitialized before calling browser.GetMainFrame() (like amaitland mentioned)






share|improve this answer


























  • I'm downvoting this as I believe calling Thread.Sleep in a tight loop should be avoided at all costs.

    – amaitland
    Nov 21 '18 at 9:19











  • yup. As you mentioned in your answer subscribing to the event is definitely the better way to do it :-)

    – ChrizzleWhizzle
    Nov 21 '18 at 9:29











  • By all means edit your answer and I'll happily change my vote.

    – amaitland
    Nov 21 '18 at 9:40
















1














Well like the error message is telling you, you should check if the browser is already initialized.



I don't know the implementation of the class ChromiumWebBrowser but I would try to first navigate to the desired url



browser.navigate(yourUrl);


and maybe check if the browser has already navigated to your url by subscribing to the event IsBrowserInitialized before calling browser.GetMainFrame() (like amaitland mentioned)






share|improve this answer


























  • I'm downvoting this as I believe calling Thread.Sleep in a tight loop should be avoided at all costs.

    – amaitland
    Nov 21 '18 at 9:19











  • yup. As you mentioned in your answer subscribing to the event is definitely the better way to do it :-)

    – ChrizzleWhizzle
    Nov 21 '18 at 9:29











  • By all means edit your answer and I'll happily change my vote.

    – amaitland
    Nov 21 '18 at 9:40














1












1








1







Well like the error message is telling you, you should check if the browser is already initialized.



I don't know the implementation of the class ChromiumWebBrowser but I would try to first navigate to the desired url



browser.navigate(yourUrl);


and maybe check if the browser has already navigated to your url by subscribing to the event IsBrowserInitialized before calling browser.GetMainFrame() (like amaitland mentioned)






share|improve this answer















Well like the error message is telling you, you should check if the browser is already initialized.



I don't know the implementation of the class ChromiumWebBrowser but I would try to first navigate to the desired url



browser.navigate(yourUrl);


and maybe check if the browser has already navigated to your url by subscribing to the event IsBrowserInitialized before calling browser.GetMainFrame() (like amaitland mentioned)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 9:43

























answered Nov 21 '18 at 9:06









ChrizzleWhizzleChrizzleWhizzle

623




623













  • I'm downvoting this as I believe calling Thread.Sleep in a tight loop should be avoided at all costs.

    – amaitland
    Nov 21 '18 at 9:19











  • yup. As you mentioned in your answer subscribing to the event is definitely the better way to do it :-)

    – ChrizzleWhizzle
    Nov 21 '18 at 9:29











  • By all means edit your answer and I'll happily change my vote.

    – amaitland
    Nov 21 '18 at 9:40



















  • I'm downvoting this as I believe calling Thread.Sleep in a tight loop should be avoided at all costs.

    – amaitland
    Nov 21 '18 at 9:19











  • yup. As you mentioned in your answer subscribing to the event is definitely the better way to do it :-)

    – ChrizzleWhizzle
    Nov 21 '18 at 9:29











  • By all means edit your answer and I'll happily change my vote.

    – amaitland
    Nov 21 '18 at 9:40

















I'm downvoting this as I believe calling Thread.Sleep in a tight loop should be avoided at all costs.

– amaitland
Nov 21 '18 at 9:19





I'm downvoting this as I believe calling Thread.Sleep in a tight loop should be avoided at all costs.

– amaitland
Nov 21 '18 at 9:19













yup. As you mentioned in your answer subscribing to the event is definitely the better way to do it :-)

– ChrizzleWhizzle
Nov 21 '18 at 9:29





yup. As you mentioned in your answer subscribing to the event is definitely the better way to do it :-)

– ChrizzleWhizzle
Nov 21 '18 at 9:29













By all means edit your answer and I'll happily change my vote.

– amaitland
Nov 21 '18 at 9:40





By all means edit your answer and I'll happily change my vote.

– amaitland
Nov 21 '18 at 9:40




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405575%2fcefsharp-make-a-post-web-api-call-win-forms-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings