This is a migrated thread and some comments may be shown as answers.

Help: Count words in Response

3 Answers 165 Views
Windows
This is a migrated thread and some comments may be shown as answers.
Damien
Top achievements
Rank 1
Damien asked on 30 May 2015, 10:57 AM

I am currently using the below script in custom rules to highlight the session green and put "found" into the custom column, if the word "hello" is found in the session.

What I am trying to workout now, is how can I adjust this to display a count of "hello" into the custom column?

 

Script:

if (oSession.utilFindInResponse("hello", false)>-1 && oSession.utilFindInResponse("startMap", false)>-1){
oSession["ui-color"] = "green"

oSession["ui-customcolumn"]="found"

 

Thanks

3 Answers, 1 is accepted

Sort by
0
Eric Lawrence
Telerik team
answered on 02 Jun 2015, 06:27 PM
Hello, Damien--

There's nothing in Fiddler to specifically handle "counting" so this becomes a more general question of "how can I count occurrences of a substring in .NET?"

if (oSession.HostnameIs("whatever.com") && oSession.ResponseHeaders.ExistsAndContains("Content-Type", "text/"))
{
   var strText: String = oSession.GetResponseBodyAsString();

   var iCount = 0;
   // Call your counting function here. See e.g. http://stackoverflow.com/a/15142776/126229 for example

   if (iCount > 0) {
     oSession["ui-color"] = "green";
     oSession["ui-customcolumn"]="found " + iCount.ToString();
   }
}

Regards,
Eric Lawrence
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Damien
Top achievements
Rank 1
answered on 28 Jun 2015, 10:50 AM

Thanks Eric, 

 

It may sound stupid, but I was unable to get one of the counting functions on http://stackoverflow.com/a/15142776/126229 to work in fiddler

the string I am looking to a count in the response is "e_resource_events_gp6_pack_currency"

 how do I get it to count the variable "strText" that I've already declared?

 Can you paste an example of the full count function including these variable and count string

0
Eric Lawrence
Telerik team
answered on 29 Jun 2015, 04:52 PM
For general programming questions like this, StackOverflow is a great place to get help. Beyond a broader set of .NET programmers there to answer question, you are also encouraged to include specific details of what you've tried so far and what the results were.

// Create a column to fill with the count of the target string
public static BindUIColumn("#String", 60)
function FillCountColumn(oS: Session): String {
   if (!oS.bHasResponse) return String.Empty;
   if (!oS.oResponse.MIMEType.ToLower().StartsWith("text/")) return String.Empty;
   return CountSubstrings(oS.GetResponseBodyAsString(),
                          "e_resource_events_gp6_pack_currency").ToString();
}

// Count occurrences of "find" in "txt". Case-sensitive!
public static function CountSubstrings(txt: String, find: String): int
{
   var num: int = 0;
   var pos:int = 0;

   if (String.IsNullOrEmpty(txt) || String.IsNullOrEmpty(find)) return 0;

   while ((pos = txt.IndexOf(find, pos)) > -1)
   {
     num++;
     pos += find.Length;
   }
 
   return num;
}

Regards,
Eric Lawrence
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Windows
Asked by
Damien
Top achievements
Rank 1
Answers by
Eric Lawrence
Telerik team
Damien
Top achievements
Rank 1
Share this question
or