How can I get the full url including characters after a # char - asp-classic

I have the following code:
<%#Language="VBSCRIPT"%>
<html>
<head>
<title>in/Test Page 2</title>
</head>
<body>
<%
response.write "Request.QueryString = """ & Request.QueryString & """<br />"
for each item in request.QueryString
response.write (item + " = " + Request.QueryString(item) + "<br>")
next
%>
</body>
</html>
When I call it with this url:
TestPage2.asp?id=1&turl=http://www.google.com?id=1&url=generic#index
Which produces this output
Request.QueryString = "id=1&turl=http://www.google.com?id=1&url=generic"
id = 1
turl = http://www.google.com?id=1url=generic
How do I get the bit after the # char in the original url? I've looked all through the Request.Servervariable's.

Short answer: You can't
It's by design.
From RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax - Section 4.1 Fragment Identifier
When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch (“#”) character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.
Basically the # is never sent to the server so you can't retrieve it from Classic ASP. The client (Internet Browser) removes the # fragment from the URI before it is sent.
Having said that it is possible to retrieve it from the client-side using JavaScript then pass the value via a form / querystring to the server where Classic ASP can retrieve and use it.

Related

Session and XMLHTTP

I have two asp pages on the same server. The first one generates XML dynamically using querystring informations and session information. The second one reads the first one using an XMLHTTP object and do things using the XML datas.
However, my problem is that the XMLHTTP request is done server-side. Thus, the session variables of the client are not accessible when the xml should be generated.
How can I do so that the page that generates the XML receives the session variables ?
Thanks.
There are many problems with what you are trying to do, not least is that it can lead a busy server to lock up entirely.
Here is the another approach.
Add a third page to your solution. This page contains simply a function which returns an XML DOMDocument. This function contains all the logic from your original xml generating page but builds the XML into the DOM (which you were probably doing all ready right?).
Now your original page simply includes this new function page, calls the function and sends the DOM to the response:
<!-- #include file="xmlFunction.asp" -->
<%
Response.ContentType = "text/xml"
Response.CharSet = "UTF-8"
GenerateXml().Save Response
%>
Your client page can now look this
<!-- #include file="xmlFunction.asp" -->
<%
. . .
Dim dom: Set dom = GenerateXml()
''# Code that uses the XML in the dom.
%>
No additional "Request to self" is needed hence no potential lock up. Since code in the xmlFunction.asp is running as part of the original request the appropriate Session object is accessible.

Classic ASP VBScript return PC name issue

EDIT - Better asked maybe I need to get the value from the VBScript variable NAME into the ASP form, any attempt to reference the variable other than print it to the screen is ignored.
this is on an intranet, only for internal use, and IE is used at the lowest security settings so the activeX runs without prompting.
I have a vbscript that when it runs returns what I am looking for - the client PC name
<script language="vbscript">
<!--
Set objNetwork = CreateObject("WScript.Network")
NAME= objNetwork.computername
document.write(NAME)
//-->
</script>
When I run the following I get the server name:
<%
Set objNetwork = CreateObject("WScript.Network")
strNAME = objNetwork.computername
response.write("Value of strNAME variable: " & strNAME & "<br>")
%>
I need the actual PC name that the VBScript returns, and I need to insert that into a form. Using the ASP I can do the fill of the form but it gives me the server name and I need the VBScript PC Client name. I can not figure out how to get the VBScript name into the ASP form.
You need client side VBScript:
<script language="vbscript">
<!--
Function SetComputerName
Set objNetwork = CreateObject("WScript.Network")
NAME=objNetwork.computername
Document.Forms("form1").Elements("computername").Value = NAME
End Function
SetComputerName()
//-->
</script>
Put the form name or ID instead of "form1" and the input element name that should contain the computer name instead of "computername".
The script will work only if placed in the end of your HTML markup, or at least after the form HTML, if you want it placed above add such thing to your body tag: <body onload="SetComputerName()">
Generate your HTML outside of your code block, like this. This is the correct syntax, but generally the client name is not exposed via browsers unless you use IE and some sort of Active-X control.
<input type="hidden" value="<%=strNAME%>" />

urlencode all querystring parameters

Is there a way to url encode the entire URL querystring without trying to urlencode each individual querystring parameters. Right now I'm having to rebuild the querystring with something like this:
foreach (string x in Page.Request.QueryString.Keys)
{
sQueryString += x + "=" + Server.UrlEncode(Request.Params.Get(x)) + "&";
}
All you should to do is to get the following value:
Page.Request.Url.Query
See:
Uri baseUri = new Uri("http://www.contoso.com/catalog/shownew.htm?date=today&<a>=<b>");
string queryString = baseUri.Query;
The queryString parameter will return ?date=today&%3Ca%3E=%3Cb%3E.
One more edit - from the MSDN:
The Query property contains any query
information included in the URI. Query
information is separated from the path
information by a question mark (?) and
continues to the end of the URI. The
query information returned includes
the leading question mark.
The query information is escaped
according to RFC 2396 by default. If
International Resource Identifiers
(IRIs) or Internationalized Domain
Name (IDN) parsing is enabled, the
query information is escaped according
to RFC 3986 and RFC 3987.
Other than using string.Format and you having an extra & at the end of your QueryString the approach above is optimal.

How can I stop asp.net encoding & in Get params?

I am using the following code to add a series of calls to the body parameter of a page in asp.net:
uxBodyTag.Attributes["onbeforeunload"] +=
"ajaxRequest('UnlockQuery.ashx?QueryID=" + queryId.ToString() +
"&UserID=" + Session["UserID"].ToString() + "');";
This is being rendered as:
<body id="uxBodyTag" onbeforeunload=
"ajaxRequest('UnlockQuery.ashx?QueryID=176&UserID=11648');">
The & means my ashx page is not retrieving the correct variables - how can I stop asp.net from doing this?
EDIT:
Using
Server.UrlEncode
gives me the following:
<body id="uxBodyTag" onbeforeunload=
"ajaxRequest('UnlockQuery.ashx%3fQueryID%3d179%26UserID%3d11648')%3b">
Which is far worse.
In HTML the ampersand needs to be encoded, always, everywhere, also in attribute values (the contents of the <script> tag is the notable exception, obviously). ASP.NET does the right thing.
Attribute values will be unencoded by the browser before it actually uses them. So your onbeforeunload attribute has a literal value of:
ajaxRequest('UnlockQuery.ashx?QueryID=176&UserID=11648');
while the HTML representation needs to have the & in place of the &. The browser usually understands the ill-encoded version as well, but an SGML parser would complain about an unknown/invalid entity named &UserID.
The behaviour you are seeing with & encoded as & is the behaviour you want. By the time the text gets to your ajaxRequest function it will have been unencoded again and everything should be fine.

How to stop .NET from encoding an XML string with XML.Serialization

I am working with some Xml Serialization in ASP.NET 2.0 in a web service. The issue is that I have an element which is defined such as this:
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)> _
Public Property COMMENTFIELD() As String
Get
Return CommentField ' This is a string
End Get
Set(ByVal value as String)
CommentField = value
End Set
End Property
Elsewhere in code I am constructing a comment and appending
as a line-break (according to the rules of the web service we are submitting to) between each 'comment', like this: (Please keep in mind that
is a valid XML entity representing character 10 (Line Feed I believe).
XmlObject.COMMENTFIELD = sComment1 & "
" & sComment2
The issue is that .NET tries to do us a favor and encode the & in the comment string which ends up sending the destination web service this: &#xA;, which obviously isn't what we want.
Here is what currently happens:
XmlObject.COMMENTFIELD = sComment1 & "
" & sComment2
Output:
<COMMENTFIELD>comment1 &#xA comment2</COMMENTFIELD>
Output I NEED:
<COMMENTFIELD>comment1
comment2</COMMENTFIELD>
The Question Is: How do I force the .NET runtime to not try and do me any favors in regards to encoding data that I already know is XML compliant and escaped already (btw sComment1 and sComment2 would already be escaped). I'm used to taking care of my XML, not depending on something magical that happens to escape all my data behind my back!
I need to be able to pass valid XML into the COMMENTFIELD property without .NET encoding the data I give it (as it is already XML). I need to know how to tell .NET that the data it is receiving is an XML String, not a normal string that needs escaped.
If you look at the XML spec section 2.4, you see that the & character in an element's text always used to indicate something escaped, so if you want to send an & character, it needs to be escaped, e.g., as & So .Net is converting the literal string you gave it into valid XML.
If you really want the web service to receive the literal text &, then .NET is doing the correct thing. When the web service processes the XML it will convert it back to the same literal string you supplied on your end.
On the other hand, if you want to send the remote web service a string with a newline, you should just construct the string with the newline:
XmlObject.COMMENTFIELD = sComment1 & "\n" & sComment2
.Net will do the correct thing to make sure this is passed correctly on the wire.
It is probably dangerous to mix two different encoding conventions within the same string. Since you have your own convention I recommend explicitly encoding the whole string when it is ready to send and explicitly decoding it on the receiving end.
Try the HttpServerUtility.HtmlEncode Method (System.Web) .
+tom

Resources