Forum Discussion

joker6413's avatar
joker6413
Occasional Contributor
15 years ago

How to simulate custom user-agent query to web application?

Hi.



I have web application that generate different response depend on mobile browser user-agent. How can i test such behaviour? I do not need any interraction. I need only make GET request and analyze the response (make some String.indexOf() tests).



With best regards.

Igor
  • joker6413's avatar
    joker6413
    Occasional Contributor
    Final version of script:




    function HttpGet(url, userAgent)

    {

    var httpObj = Sys.OleObject("MSXML2.XMLHTTP");

       httpObj.open("GET", url, true);

    httpObj.setRequestHeader("User-Agent", userAgent); // set user agent 

    httpObj.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); /// do not cache the response

       httpObj.send();





       while((httpObj.readyState != 4) && (httpObj.readyState != 'complete')) 

    {

          Delay(300);

       }

       switch(httpObj.status) 

    {

       case 200:

          case 302:

            if (httpObj.responseText.lenght == 0) //!= expectedObjectData) 

     {

              Log.Message("The " + link + " link is valid", httpObj.responseText);

              return false;

            }

     return httpObj.responseText;

            break;

           

          default:

            Log.Message("The " + link + " link was not found, the returned status: " +

              httpObj.status, httpObj.responseText);

            return false;

    }

    }


  • Matroskin's avatar
    Matroskin
    Occasional Contributor

    hello


    can i scrips same function by using delphiscript?


    i tried it, but  "httpObj.open("GET", url, true);" calls "unknown error"

  • Hi Vladimir,



    The following code is a DelphiScript version of the function. Note that I've modified a bit the original program logic of the code posted earlier by Igor. However, you can change the code of the function if you need and implement your own program logic in it.





    function HttpGet(url, userAgent): boolean;

    var

      httpObj: OleVariant;

    begin

      httpObj := Sys.OleObject['MSXML2.XMLHTTP'];

      httpObj.open('GET', url, true);

      httpObj.setRequestHeader('User-Agent', userAgent); // set user agent

      httpObj.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT'); /// do not cache the response

      httpObj.send();

      while(httpObj.readyState <> 4) do

        Delay(300);

        

      case httpObj.status of 

        200, 302 :

          if Length(httpObj.responseText) <> 0 then

            begin

              Log.Message('The ' + url + ' link is valid', httpObj.responseText);

                Result := false;

            end

          else

            Result := true;

      else

        begin

          Log.Message('The ' + url + ' link was not found, the returned status: ' +

                aqConvert.VarToStr(httpObj.status), httpObj.responseText);

          Result := false;

        end

      end

    end;





    Does this help?
  • Matroskin's avatar
    Matroskin
    Occasional Contributor

    thanks for your answer.


    when i posted message about error, tried to execute this script:


    procedure RequestTest;  


    var i:olevariant;  

    begin

    try

    i:= Sys.OleObject('MSXML2.XMLHTTP');

    i.open('GET', '192.168.0.106/Some.Service/action.aspx?src=someSourse&dest=someDest&message=someMessage', True);


    delay(5000);

    Log.Event(i.readyState);

    except

    Log.Error('Exception', ExceptionMessage)

    end;

    end;


    at string, which contains " i.open('GET', '192.16.." i got an error. you proposed me almost same code, excluding minor parameters. right now  
    this function
    is written , but with making request by telnet. i hope is temprorary )

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    I'm guessing there's something wrong with your URL.  Try actually using the "http://" designator at the beginning of the URL and see if that corrects it.