Link to home
Create AccountLog in
Avatar of jimbona27
jimbona27Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Request.ServerVariables("HTTP_REFERER") get a keys value

hi there,
I need to get the value of a key within the Request.ServerVariables("HTTP_REFERER") variable.  

if I have a variable called test within the http_referer how can I get the value of the it out?  

test.aspx?a=1&b=2&c=3&d=4&e=5&f=6

find e, retrieve value 5

Avatar of Pratima
Pratima
Flag of India image

Request("e") will return value 5
Avatar of jimbona27

ASKER

this works although I need to return the previous querystring value not the present one

if I have

test.aspx?a=1&b=2&c=3&d=4&e=5&f=6

then loads this, not the f value changing

test.aspx?a=1&b=2&c=3&d=4&e=5&f=10

i need to get the value f=6 out, hence using HTTP_REFERER.  

using request("f") displays 10, rather than 6??
To find key/values for a different path than the current executing page, you can create a custom HttpRequest object. The use the usual querystring etc.

                    string sFile = "test.aspx?a=1&b=2&c=3&d=4&e=5&f=6";
                    string sQ = "";
                    int iQM = sFile.IndexOf("?");
                    if(iQM>0)
                    {
                          sQ = sFile.Substring(iQM+1);
                          sFile = sFile.Substring(0, iQM);
                    }
                    System.Web.HttpRequest hr = new HttpRequest(sFile, "http://localhost/", sQ);
                    Response.Write(hr.QueryString["f"]);
ok you have
test.aspx?a=1&b=2&c=3&d=4&e=5&f=6

you can store it in string

str="test.aspx?a=1&b=2&c=3&d=4&e=5&f=6"

by using spilt first by ? , then by & 
you can get array of a=1 , b=2 like this

        Dim sFile As String = Request.ServerVariables("HTTP_REFERER")
        Dim sQ As String = ""
        Dim iQM As Integer

        Int(iQM = sFile.IndexOf("?"))

        If iQM > 0 Then
            sQ = sFile.Substring(iQM + 1)
            sFile = sFile.Substring(0, iQM)
        End If

following lines bum out - cannot be used as an expression
        System.Web.HttpRequest(hr = New HttpRequest(sFile, "http://localhost/", sQ))
       'Response.Write(hr.QueryString["f"])
I used C# - not VB.Net. QueryString cannot handle the square brackets, make it like:

Response.Write(hr.QueryString("f"))
Dim hr as System.Web.HttpRequest = New HttpRequest(sFile, "http://localhost/", sQ)
Response.Write(hr.QueryString("f"))

I don't like VB.Net :(

        Dim qs As String = Request.ServerVariables("HTTP_REFERER")

        Dim bits() As String
        Dim i As Integer

        bits = Split(qs, "?")
        bits = Split(bits(1), "&")

        For i = 0 To UBound(bits)
            Response.Write(bits(i) & "<BR>")
        Next i

        Response.Write(bits(1))

this works.

displays all the keys within the original querystring.

how can I get the value out of it for a given key?
Dim qs As String = Request.ServerVariables("HTTP_
Dim bits() As String
Dim i As Integer

bits = Split(qs, "?")
bits = Split(bits(1), "&")

For i = 0 To UBound(bits)
    Response.Write(i & " " & bits(i) & "<BR>")
Next i

bits(6) holds what I want but I cant take this for granted, how can I say request(5) within my bits array? or something to that affect?

           
sorry not request(5) but request(keyname)

any ideas?

many thanks
Did you even look at my postings?
oh sorry, just tried again with the vb.net change and it does not display anything?

        Dim sFile As String = Request.ServerVariables("HTTP_REFERER")
        Dim sQ As String = ""
        Dim iQM As Integer

        Int(iQM = sFile.IndexOf("?"))

        If iQM > 0 Then
            sQ = sFile.Substring(iQM + 1)
            sFile = sFile.Substring(0, iQM)
        End If

        Dim hr As System.Web.HttpRequest = New HttpRequest(sFile, "localaddress/", sQ)
        Response.Write(hr.QueryString("f"))
iQM = sFile.IndexOf("?"))

iQM is empty?
ASKER CERTIFIED SOLUTION
Avatar of CJ_S
CJ_S
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
this seems to work so many thanks.  i'll just test a few things.
does this change anything with the request.querystring variables?  thanks.
This way you have created a new HttpWebRequest object - and can be used in conjunction with the normal Request object.