I have a Windows Form application that has Web browser control. The page I access has input parameters in the Request Body.

Request Body looks like below and I want to get values for Doc_Name, Area and SubArea.
SearchOrder=2&Start=0&Count=-1&Query=(%5BDoc_Name%5D%3DABC+OR+%5BArea%5D%3DXYZ+OR+%5BSubArea%5D%3DQRS)
How do I extract from this?

Dani AI

Generated

As described, the payload is form-urlencoded. As pointed out, percent-encoding must be decoded, but the substring-index approach shown in the replies is brittle (breaks if field order, spacing or separators change). A more robust two-step approach is recommended: (1) parse the top-level form body into name/value pairs, (2) parse the decoded value of the Query field for the bracketed field/value pairs.

The following C# pattern is concise and tolerant of ordering and spacing. It uses HttpUtility.ParseQueryString to turn the form body into keys, then a Regex to extract bracketed keys and their values from the Query value.

using System.Text.RegularExpressions;
using System.Web; // add reference to System.Web if needed

string rawBody = /* the raw request body string */;
var form = HttpUtility.ParseQueryString(rawBody);   // decodes percent-encoding and plus->space
string queryPart = form["Query"];                   // e.g. "([Doc_Name]=ABC OR [Area]=XYZ OR [SubArea]=QRS)"

if (!string.IsNullOrEmpty(queryPart)) {
    string inner = queryPart.Trim();
    if (inner.StartsWith("(") && inner.EndsWith(")")) inner = inner.Substring(1, inner.Length - 2);

    var rx = new Regex(@"\[(?<key>[^\]]+)\]\s*=\s*(?<val>.*?)(?=(\s+OR\s+)|$)", RegexOptions.IgnoreCase);
    foreach (Match m in rx.Matches(inner)) {
        string key = m.Groups["key"].Value;   // Doc_Name, Area, SubArea
        string val = m.Groups["val"].Value.Trim();
        // store or map key->val
    }
}

Notes and troubleshooting:

  • If System.Web is unavailable, split on '&' and '=' and call Uri.UnescapeDataString on each piece before using the regex.
  • Trim surrounding parentheses, normalize whitespace and separator words (OR/AND), and use case-insensitive regex.
  • If the intention is to capture the actual POST body from a WinForms WebBrowser before it is sent, the control does not expose it directly; options are reading form elements from WebBrowser.Document before submit or hooking the underlying ActiveX BeforeNavigate2/postData callback to inspect the raw bytes.

%5B, %5D, %3D are ASCII values.

%5B = '[' (left square bracket)
%5D = ']' (right square bracket)
%3D = '=' (equals)

ASCII Table

Below is a method of extracting the data:

string inputData = @"SearchOrder=2&Start=0&Count=-1&Query=(%5BDoc_Name%5D%3DABC+OR+%5BArea%5D%3DXYZ+OR+%5BSubArea%5D%3DQRS)";

string area = string.Empty;
string docName = string.Empty;
string subArea = string.Empty;
string userParams = string.Empty;

//get substring that contains the parameters
userParams = inputData.Substring(inputData.IndexOf("%5BDoc_Name%5D%3D"));

//get value for DocName
docName = userParams.Substring(userParams.IndexOf("%5BDoc_Name%5D%3D") + "%5BDoc_Name%5D%3D".Length, userParams.IndexOf("+OR+%5BArea%5D%3D") - (userParams.IndexOf("%5BDoc_Name%5D%3D") + "%5BDoc_Name%5D%3D".Length)).Trim();

//get value for Area
area = userParams.Substring(userParams.IndexOf("%5BArea%5D%3D") + "%5BArea%5D%3D".Length, userParams.IndexOf("+OR+%5BSubArea%5D%3D") - (userParams.IndexOf("%5BArea%5D%3D") + "%5BArea%5D%3D".Length)).Trim();

//get value for SubArea
subArea = userParams.Substring(userParams.IndexOf("%5BSubArea%5D%3D") + "%5BSubArea%5D%3D".Length, userParams.IndexOf(")") - (userParams.IndexOf("%5BSubArea%5D%3D") + "%5BSubArea%5D%3D".Length)).Trim();

The above code is difficult to read, however. So to make it a little easier to read, one could decode the URL before processing it. To do this, one can use:
HttpUtility.UrlDecode Method

This will change:

SearchOrder=2&Start=0&Count=-1&Query=(%5BDoc_Name%5D%3DABC+OR+%5BArea%5D%3DXYZ+OR+%5BSubArea%5D%3DQRS)

Into:

SearchOrder=2&Start=0&Count=-1&Query=([Doc_Name]=ABC OR [Area]=XYZ OR [SubArea]=QRS)

Note: HttpUtility.UrlDecode is not in the client version of .NET 4.0. If using, .NET 4.0, one needs to use the full version.

To use System.Web.HttpUtility.UrlDecode:

Add Reference to System.Web:

  • Click "Project" (in menu bar)
  • Select "Add Reference"
  • Click ".NET" tab
  • Select "System.Web"

Add using statement:

  • using System.Web;

Then, the process is similar to the above code:

string inputData = @"SearchOrder=2&Start=0&Count=-1&Query=(%5BDoc_Name%5D%3DABC+OR+%5BArea%5D%3DXYZ+OR+%5BSubArea%5D%3DQRS)";

string area = string.Empty;
string decodedUrl = string.Empty;
string docName = string.Empty;
string subArea = string.Empty;
string userParams = string.Empty;

//decode inputData and put result in decodedUrl
decodedUrl = System.Web.HttpUtility.UrlDecode(inputData);

//get substring that contains the parameters
userParams = decodedUrl.Substring(decodedUrl.IndexOf("[Doc_Name]"));

//get value for DocName
docName = userParams.Substring(userParams.IndexOf("[Doc_Name]=") + "[Doc_Name]=".Length, userParams.IndexOf("OR [Area]") - (userParams.IndexOf("[Doc_Name]=") + "[Doc_Name]=".Length)).Trim();

//get value for Area
area = userParams.Substring(userParams.IndexOf("[Area]=") + "[Area]=".Length, userParams.IndexOf("OR [SubArea]") - (userParams.IndexOf("[Area]=") + "[Area]=".Length)).Trim();

//get value for SubArea
subArea = userParams.Substring(userParams.IndexOf("[SubArea]=") + "[SubArea]=".Length, userParams.IndexOf(")") - (userParams.IndexOf("[SubArea]=") + "[SubArea]=".Length)).Trim();

Alternatively, you can do the following:

//get value for DocName
int docNameStartIndex = userParams.IndexOf("[Doc_Name]=") + "[Doc_Name]=".Length;

int docNameLength = userParams.IndexOf("OR [Area]") - (userParams.IndexOf("[Doc_Name]=") + "[Doc_Name]=".Length);

docName = userParams.Substring(docNameStartIndex, docNameLength).Trim();

instead of:

//get value for DocName
docName = userParams.Substring(userParams.IndexOf("[Doc_Name]=") + "[Doc_Name]=".Length, userParams.IndexOf("OR [Area]") - (userParams.IndexOf("[Doc_Name]=") + "[Doc_Name]=".Length)).Trim();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.