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?

%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.