...

View Full Version : Modify HTTP header



Sman5109
10-12-2007, 12:21 AM
Hello,

I know nothing about ASP but I made and coded my schools website and theres no PHP installed so I have to go with ASP. What I want to do is do the exact same thing the PHP code below does but in ASP. Can I do this?

<?php

if ((isset($_SERVER["HTTP_ACCEPT"]) and

stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) or

stristr($_SERVER["HTTP_USER_AGENT"], "W3C_Validator") )

{

header("Content-type: application/xhtml+xml");

print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");

}

else

{

header("Content-type: text/html; charset=utf-8");

}

?>

Thank you very much :)

Roelf
10-12-2007, 06:09 AM
<%
'setting response.buffer makes sure nothing is sent to the browser before the entire script is executed. Otherwise headers dannot be changed
Response.Buffer = True

'check for the ServerVariables HTTP_ACCEPT and HTTP_USER_AGENT
If Request.ServerVariables("HTTP_ACCEPT") = "application/xhtml+xml" Or
Request.ServerVariables("HTTP_USER_AGENT") = "W3C_Validator" Then
'SetHeader Name, Value
Response.SetHeader "Content-type", "application/xhtml+xml"
'add the string to the output stream, escaping double quotes by doubling them
Response.Write "<?xml version=""1.0"" encoding=""utf-8""?>" & VBCrLf
Else
Response.SetHeader "Content-type", "text/html; charset=utf-8"
End If

%>

(just translated it, did not test)

Sman5109
10-12-2007, 07:19 AM
Nope,


Microsoft VBScript compilation error '800a03ea'
Syntax error
/flclfan/index.asp, line 6
If Request.ServerVariables("HTTP_ACCEPT") = "application/xhtml+xml" Or
----------------------------------------------------------------------^

Spudhead
10-12-2007, 10:03 AM
Put the line below it on the same line as the one that ends "Or".

Sman5109
10-13-2007, 01:38 AM
Ok, now i get this error.


Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'Response.SetHeader'
/flclfan/index.asp, line 12

Roelf
10-13-2007, 03:39 PM
You are right, it must be:

Response.AddHeader "Content-type", "application/xhtml+xml"

Sman5109
10-13-2007, 07:17 PM
<%
'setting response.buffer makes sure nothing is sent to the browser before the entire script is executed. Otherwise headers dannot be changed
Response.Buffer = True

'check for the ServerVariables HTTP_ACCEPT and HTTP_USER_AGENT
If Request.ServerVariables("HTTP_ACCEPT") = "application/xhtml+xml" Or Request.ServerVariables("HTTP_USER_AGENT") = "W3C_Validator" Then
'AddHeader Name, Value
Response.AddHeader "Content-type", "application/xhtml+xml"
'add the string to the output stream, escaping double quotes by doubling them
Response.Write "<?xml version=""1.0"" encoding=""utf-8""?>" & VBCrLf
Else
Response.AddHeader "Content-type", "text/html; charset=utf-8"
End If

%>

Ok, theres the code, I now get no errors but its not doing anything. The page is still being served as text/html and its not writing "<?xml version="1.0" encoding="utf-8"?>". What do i do now?

Roelf
10-13-2007, 08:32 PM
Well, you can try to output the values for the variables you are checking by:


Response.Write "Var HTTP_ACCEPT: " & Request.ServerVariables("HTTP_ACCEPT") & "<br />"
Response.Write "Var HTTP_USER_AGENT: " & Request.ServerVariables("HTTP_USER_AGENT") & "<br />"
to see what you are comparing with

Sman5109
10-13-2007, 10:12 PM
Ok, heres my results:


Var HTTP_ACCEPT: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Var HTTP_USER_AGENT: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7

Can you tell me whats wrong with the code and why its not working?

Roelf
10-13-2007, 10:43 PM
Ok, heres my results:


Var HTTP_ACCEPT: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Var HTTP_USER_AGENT: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7

Can you tell me whats wrong with the code and why its not working?

nothing is wrong with the code. You compare the HTTP_ACCEPT with the string
"application/xhtml+xml". The HTTP_ACCEPT you are getting is:
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" So there is no exact match here. The same goes for the HTTP_USER_AGENT. You compare with:
"W3C_Validator", but you are getting:
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7" So, there is no exact match either. The code is doing what it is supposed to do and goes inte the Else branch of your If statement.

You probably want to check if the string "application/xhtml+xml" appears somewhere within the HTTP_ACCEPT variable. Then you should use the InStr() function. This function checks to see if the second provided string appears somewhere in the first provided string and returns the position where it starts. So if it returns a number larger than 0 the second string appears in the first.

If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/xhtml+xml") > 0 OR InStr(Request.ServerVariables("HTTP_USER_AGENT"), "W3C_Validator") > 0 Then
etcetera

Sman5109
10-13-2007, 11:02 PM
But what that PHP code does is serve the page as application/xhtml+xml to browsers that support it and the W3C Validator and add the xml version line, and serve the page as text/html to all other browsers that do not support application/xhtml+xml.

My browser supports application/xhtml+xml and thus if the asp code does what it should i should see that the page is being served as application/xhtml+xml, and the xml line should print/write in there.

Roelf
10-13-2007, 11:06 PM
sorry for not exactly knowing what the stristr() function in php does. But i think my last suggestion with the InStr() function in VBScript does what you need.....

Have you tried that yet?

Sman5109
10-13-2007, 11:19 PM
I added that line heres the code:

<%
Response.Buffer = True
If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/xhtml+xml") > 0 OR InStr(Request.ServerVariables("HTTP_USER_AGENT"), "W3C_Validator") > 0 Then
Response.AddHeader "Content-type", "application/xhtml+xml"
Response.Write "<?xml version=""1.0"" encoding=""utf-8""?>" & VBCrLf
Else
Response.AddHeader "Content-type", "text/html; charset=utf-8"
End If
%>Is this code right?

It now prints the "<?xml version=""1.0"" encoding=""utf-8""?>" but it is still not being served in ...../xhtml+xml.

Edit: The W3C validator is getting application/xhtml+xml but im still not.

Roelf
10-15-2007, 02:40 PM
To be honest, i don't know why that happens. The fact that the xml tag is printed is an indication that the correct branch of the if then else statement is entered. But the AddHeader seems to fail for your browser. I don't know why that is. Sorry

Spudhead
10-15-2007, 02:46 PM
AddHeader adds a NEW header to the HTTP response. It won't replace an existing one. ContentType is a header that always gets set, and it defaults to text/html. If you want to change it, use Response.ContentType.

Sman5109
10-15-2007, 06:13 PM
AddHeader adds a NEW header to the HTTP response. It won't replace an existing one. ContentType is a header that always gets set, and it defaults to text/html. If you want to change it, use Response.ContentType.
Great. So what will the asp code be changed to?

Spudhead
10-16-2007, 02:39 PM
It will be changed to Response.ContentType.

Sman5109
10-16-2007, 06:23 PM
I changed them to that but im getting the error:

Microsoft VBScript runtime error '800a01c3'
Object not a collection: 'Response.ContentType'
/flclfan/index.asp, line 4
Heres the code


<%
Response.Buffer = True
If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/xhtml+xml") > 0 OR InStr(Request.ServerVariables("HTTP_USER_AGENT"), "W3C_Validator") > 0 Then
Response.ContentType "Content-type", "application/xhtml+xml"
Response.Write "<?xml version=""1.0"" encoding=""utf-8""?>" & VBCrLf
Else
Response.ContentType "Content-type", "text/html; charset=utf-8"
End If
%>

Roelf
10-16-2007, 08:28 PM
hi, what you can do is do a search for Response.ContentType (http://www.devguru.com/technologies/asp/9149.asp), then you will find out that you should do it like:
Response.ContentType = "application/xhtml+xml"

:D

Sman5109
10-16-2007, 11:58 PM
hi, what you can do is do a search for Response.ContentType (http://www.devguru.com/technologies/asp/9149.asp), then you will find out that you should do it like:
Response.ContentType = "application/xhtml+xml"

:D
I did that but just forgot the "=". :D. It works now :).
Thank you everyone who helped in this. Its just that i hate ASP because it sucks and i hate microsoft. So im not going to try to learn php and let asp die :)

One last favor, can someone with IE6 look at this ( http://www3.webng.com/flclfan/ ) page and make sure its being served in text/html?

Thank you. :)

Spudhead
10-17-2007, 09:57 AM
If you need to install multiple versions of IE on your pc you might find this useful: http://tredosoft.com/Multiple_IE

FWIW, I'm geting this in IE6:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

My user agent is showing as:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)

Sman5109
10-19-2007, 06:32 PM
If you need to install multiple versions of IE on your pc you might find this useful: http://tredosoft.com/Multiple_IE

FWIW, I'm geting this in IE6:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

My user agent is showing as:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Thank you everyone. Everything works :)

BTW: Multiple_IE does not work in Vista (I use) and i really dont want to install it in Ubuntu (I also use).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum