Help: Count words in Response

4 posts, 0 answers
  1. Damien
    Damien avatar
    2 posts
    Member since:
    May 2015

    Posted 30 May 2015 Link to this post

    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

  2. Eric Lawrence
    Admin
    Eric Lawrence avatar
    833 posts

    Posted 02 Jun 2015 Link to this post

    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
  3. Damien
    Damien avatar
    2 posts
    Member since:
    May 2015

    Posted 28 Jun 2015 in reply to Eric Lawrence Link to this post

    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

  4. Eric Lawrence
    Admin
    Eric Lawrence avatar
    833 posts

    Posted 29 Jun 2015 Link to this post

    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
Back to Top