Skip to content

Commit

Permalink
Add IWebBrowser.LoadUrlWithPostData extension method (in WebBrowserEx…
Browse files Browse the repository at this point in the history
…tensions) - simplifies calling LoadRequest

Add IPostData.AddData extension method (in PostDataExtensions)
WPF Example change from calling LoadRequest to using new LoadUrlWithPostData extension method
  • Loading branch information
amaitland committed May 26, 2017
1 parent d8520e8 commit b2143f6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
10 changes: 2 additions & 8 deletions CefSharp.Wpf.Example/ViewModels/BrowserTabViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,9 @@ private void Go()

public void LoadCustomRequestExample()
{
var frame = WebBrowser.GetMainFrame();
var postData = System.Text.Encoding.Default.GetBytes("test=123&data=456");

//Create a new request knowing we'd like to use PostData
var request = frame.CreateRequest(initializePostData:true);
request.Method = "POST";
request.Url = "custom://cefsharp/PostDataTest.html";
request.PostData.AddData("test=123&data=456");

frame.LoadRequest(request);
WebBrowser.LoadUrlWithPostData("https://cefsharp.com/PostDataTest.html", postData);
}
}
}
19 changes: 19 additions & 0 deletions CefSharp/PostDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,24 @@ public static void AddData(this IPostData postData, string data, Encoding encodi

postData.AddElement(element);
}

/// <summary>
/// Add a new <see cref="IPostDataElement"/> that represents the key and value
/// </summary>
/// <param name="postData">Post Data</param>
/// <param name="bytes">byte array that represents the post data</param>
public static void AddData(this IPostData postData, byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException("bytes");
}

var element = postData.CreatePostDataElement();

element.Bytes = bytes;

postData.AddElement(element);
}
}
}
35 changes: 35 additions & 0 deletions CefSharp/WebBrowserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Globalization;
using CefSharp.Internals;
using System.IO;
using System.Collections.Specialized;

namespace CefSharp
{
Expand Down Expand Up @@ -220,6 +221,40 @@ public static void ExecuteScriptAsync(this IWebBrowser browser, string script)
}
}

/// <summary>
/// Loads
/// Creates a new instance of IRequest with the specified Url and Method = POST
/// </summary>
/// <param name="browser"></param>
/// <param name="url"></param>
/// <param name="postDataBytes"></param>
/// <param name="contentType"></param>
/// <remarks>This is an extension method</remarks>
public static void LoadUrlWithPostData(this IWebBrowser browser, string url, byte[] postDataBytes, string contentType = null)
{
using (var frame = browser.GetMainFrame())
{
ThrowExceptionIfFrameNull(frame);

//Initialize Request with PostData
var request = frame.CreateRequest(initializePostData:true);

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

request.PostData.AddData(postDataBytes);

if(!string.IsNullOrEmpty(contentType))
{
var headers = new NameValueCollection();
headers.Add("Content-Type", contentType);
request.Headers = headers;
}

frame.LoadRequest(request);
}
}

/// <summary>
/// Load the string contents with the specified dummy url. Web security restrictions may not behave as expected.
/// </summary>
Expand Down

0 comments on commit b2143f6

Please sign in to comment.