Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
DotNetBrowser API provides functionality that allows intercepting POST / PUT / PATCH requests , accessing and modifying POST / PUT / PATCH upload data before it will be sent to a web server. POST/PUT/PATCH upload data can be one of the following types:
- PLAIN_TEXT
- BYTES
- FORM_URL_ENCODED
- MULTIPART_FORM_DATA
Depending on the upload data type, you can use different strategies for accessing and modifying upload data. The following sample demonstrates how to do this:
C#
using DotNetBrowser; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PostDataSample { /// <summary> /// This sample demonstrates how to read and modify POST parameters using NetworkDelegate. /// </summary> class Program { static void Main(string[] args) { LoggerProvider.Instance.LoggingEnabled = true; LoggerProvider.Instance.ConsoleLoggingEnabled = true; Browser browser = BrowserFactory.Create(); BrowserContext browserContext = browser.Context; NetworkService networkService = browserContext.NetworkService; networkService.NetworkDelegate = new SampleNetworkDelegate(); browser.LoadURL(new LoadURLParams("http://localhost/", "key=value", "Content-Type:text/plaint\n")); } private class SampleNetworkDelegate : DefaultNetworkDelegate { public override void OnBeforeURLRequest(BeforeURLRequestParams parameters) { if ("POST" == parameters.Method) { PostData post = parameters.PostData; PostDataContentType contentType = post.ContentType; if (contentType == PostDataContentType.FORM_URL_ENCODED) { FormData postData = (FormData)post; postData.SetPair("key1", "value1", "value2"); postData.SetPair("key2", "value2"); } else if (contentType == PostDataContentType.MULTIPART_FORM_DATA) { MultipartFormData postData = (MultipartFormData)post; postData.SetPair("key1", "value1", "value2"); postData.SetPair("key2", "value2"); postData.SetFilePair("file3", "C:\\Test.zip"); } else if (contentType == PostDataContentType.PLAIN_TEXT) { RawData postData = (RawData)post; postData.Data = "raw data"; } else if (contentType == PostDataContentType.BYTES) { BytesData data = (BytesData)post; data.Data = Encoding.UTF8.GetBytes("My data"); } parameters.PostData = post; } } } } }
VB.NET
Imports System.Text Imports DotNetBrowser Module Module1 Sub Main() LoggerProvider.Instance.LoggingEnabled = True LoggerProvider.Instance.ConsoleLoggingEnabled = True Dim browser As Browser = BrowserFactory.Create() Dim browserContext As BrowserContext = browser.Context Dim networkService As NetworkService = browserContext.NetworkService networkService.NetworkDelegate = New SampleNetworkDelegate() browser.LoadURL(New LoadURLParams("http://localhost/", "key=value", "Content-Type:text/plaint\n")) End Sub Friend Class SampleNetworkDelegate Inherits DefaultNetworkDelegate Overrides Sub OnBeforeURLRequest(parameters As BeforeURLRequestParams) If ("POST" Is parameters.Method) Then Dim post As PostData = parameters.PostData Dim contentType As PostDataContentType = post.ContentType If (contentType = PostDataContentType.FORM_URL_ENCODED) Then Dim postData As FormData = CType(post, FormData) postData.SetPair("key1", "value1", "value2") postData.SetPair("key2", "value2") ElseIf (contentType = PostDataContentType.MULTIPART_FORM_DATA) Then Dim postData As MultipartFormData = CType(post, MultipartFormData) postData.SetPair("key1", "value1", "value2") postData.SetPair("key2", "value2") postData.SetFilePair("file3", "C:\\Test.zip") ElseIf (contentType = PostDataContentType.PLAIN_TEXT) Then Dim postData As RawData = CType(post, RawData) postData.Data = "raw data" ElseIf (contentType = PostDataContentType.BYTES) Then Dim data As BytesData = CType(post, BytesData) data.Data = Encoding.UTF8.GetBytes("My data") End If parameters.PostData = post End If End Sub End Class End Module