CefSharp make a POST web api call Win Forms c#
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
add a comment |
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
Subscribe to theIsBrowserInitializedChangedevent and check cefsharp.github.io/api/67.0.0/html/…
– amaitland
Nov 21 '18 at 9:20
YourNavigatemethod never gives the web page time to actually load before attempting the call toGetTextAsync, similar problem with your use ofEvaluateScriptAsync. You should read github.com/cefsharp/CefSharp/wiki/…
– amaitland
Nov 21 '18 at 9:22
add a comment |
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
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
c# winforms post cefsharp
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 theIsBrowserInitializedChangedevent and check cefsharp.github.io/api/67.0.0/html/…
– amaitland
Nov 21 '18 at 9:20
YourNavigatemethod never gives the web page time to actually load before attempting the call toGetTextAsync, similar problem with your use ofEvaluateScriptAsync. You should read github.com/cefsharp/CefSharp/wiki/…
– amaitland
Nov 21 '18 at 9:22
add a comment |
Subscribe to theIsBrowserInitializedChangedevent and check cefsharp.github.io/api/67.0.0/html/…
– amaitland
Nov 21 '18 at 9:20
YourNavigatemethod never gives the web page time to actually load before attempting the call toGetTextAsync, similar problem with your use ofEvaluateScriptAsync. 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
add a comment |
1 Answer
1
active
oldest
votes
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)
I'm downvoting this as I believe callingThread.Sleepin 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
I'm downvoting this as I believe callingThread.Sleepin 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
add a comment |
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)
I'm downvoting this as I believe callingThread.Sleepin 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
add a comment |
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)
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)
edited Nov 21 '18 at 9:43
answered Nov 21 '18 at 9:06
ChrizzleWhizzleChrizzleWhizzle
623
623
I'm downvoting this as I believe callingThread.Sleepin 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
add a comment |
I'm downvoting this as I believe callingThread.Sleepin 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Subscribe to the
IsBrowserInitializedChangedevent and check cefsharp.github.io/api/67.0.0/html/…– amaitland
Nov 21 '18 at 9:20
Your
Navigatemethod never gives the web page time to actually load before attempting the call toGetTextAsync, similar problem with your use ofEvaluateScriptAsync. You should read github.com/cefsharp/CefSharp/wiki/…– amaitland
Nov 21 '18 at 9:22