Different result about classic asp for query string - asp-classic

I am new in classic asp.
I added this two line on the top of document
<%# Language=VBScript CODEPAGE=950 %>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
And I written some simple code
Dim str0
str0 = Request.ServerVariables("QUERY_STRING")
Response.write("Orginal string :"&str0)
And the query string contain the Chinese
http://www.mytest.com/test.asp?as=更
The output of different browser
Chrome:
Orginal string :as=%E6%9B%B4
IE:
Orginal string :as=更
May I know how can I solve this problem? Thanks

Related

My ASP.NET results are different

I am developing an ASP.NET project for our intranet which retrieves client's MachineName and IPAddress and I am using the following code.
ajax/default.aspx.vb
Public Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Request.QueryString.Count > 0 Then
Select Case Request.QueryString("eval")
Case Else
Dim type As Type = type.GetType("ajax_default")
Dim method As MethodInfo = type.GetMethod("zzfxn_" & Request.QueryString("eval").ToString, BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance)
If method IsNot Nothing Then
method.Invoke(Me, New Object() {Request})
End If
End Select
End If
End Sub
Public Sub zzfxn_zGetClientInfo(ByVal r As System.Web.HttpRequest)
Response.Write("Client IP Address: " & Request.UserHostName & "|" & Request.UserHostAddress & "<br />")
Response.Write("Client Machine Name: " & System.Net.Dns.GetHostEntry(Request.UserHostName).HostName.Split(".")(0))
End Sub
test/Default.aspx
<%# Page Language="VB" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">.hid{display: none;}</style>
</head>
<body>
<form id="form1" name="form1" runat="server">
<div>
<p>My client info is: <%= InvokeAjax("?eval=zGetClientInfo")%></p>
</div>
</form>
<script type="text/VB" runat="server">
Protected Function InvokeAjax(ByVal params As String) As String
Dim wc As New System.Net.WebClient
wc.Headers.Add("user-agent", Request.UserAgent)
Dim ru As String = Request.Url.ToString.Remove(Request.Url.ToString.IndexOf("/test")) & "/ajax/default.aspx"
Return wc.DownloadString(ru & params).ToString
End Function
</script>
</body>
</html>
I deployed the project to the test-server Windows Server 2000, ASP.NET 4 and I got the following results.
Running/viewing ajax/Default.aspx?eval=zGetClientInfo results to:
Client IP Address: 10.20.10.5|10.20.10.5
Client Machine Name: devctr
Running/viewing test/Default.aspx results to:
My client info is: Client IP Address: 10.20.10.60|10.20.10.60
Client Machine Name: test-server
10.20.10.5 or devctr is my machine so viewing directly to ajax/default.aspx returns the exact/true content, while 10.20.10.60 or test-server is my host/server.
Can someone please explain to me why they differ?
And how can I achieve the results of ajax/defaut.aspx to be exactly the same for test/Default.aspx?
In the test/Default.aspx example, you are creating a web request that is originating on the server, hence the "runat='server'" parameter. If you are trying to create an ajax request that originates from the client, you will need to use a client side script like javascript. You could use jQuery to do this by doing something like:
$.ajax({
url: "/?eval=zGetClientInfo"
}).done(function(data) {
//Set your html here
});
If you are really going to do this, however, I would consider looking into ASP.NET Web API. Your solution will be much more robust if you return your results in some sort of JSON or XML format, which web API will do.

Load a set of variables from a SQL database - language ASP.NET VB

I am trying to translate an old "Classic ASP" page to ASP.NET page. I have limited ASP.NET knowledge.
I am reading some data from a SQL server.
I have a basic question, but so far I was not able to successfully implement any working solution.
The code attached is working well; shows the data I need.
I would like to have a similar code structure but this time I would like to assign the data from SQL to variables (I do not need to print these output values on the screen):
title (output of SQL query) ---> "titleV" variable (string)
father (output of SQL query) ---> "fatherV" variable (string)
age (output of SQL query) ---> "ageV" variable (number)
If possible, please suggest to me a piece of code I can test.
Thank you.
<%# Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=NewOleDbConnection("connection string to sql server")
dbconn.Open()
sql="SELECT * FROM photos"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>
<!DOCTYPE html>
<html>
<body>
<form runat="server">
<asp:DataList id="customers" runat="server">
<ItemTemplate>
<%#Container.DataItem("title")%> in
<%#Container.DataItem("father")%> with
<%#Container.DataItem("age")%> years
</ItemTemplate>
</asp:DataList>
</form>
</body></html>
You can save one record in above 3 variables because you are not using array type or collections. You can assign the data from the table to your variables using the following code.
sub page_load()
dbconn = new oledbconnection("conn str")
dbconn.Open()
sql="SELECT * FROM photos"
dim adpt as OledbDataAdapter
dim rs as new DataTable
adpt = new OledbDataAdapter(sql,dbconn)
adpt.fill(rs)
customers.DataSource = rs
customers.DataBind()
dim titleV, fatherV As String
dim ageV as integer
titleV = rs.Rows(0).Item(0) 'Rows(0).Item(0) gives you first row's first field value.
fatherV = rs.Rows(0).Item(1)
ageV = rs.Rows(0).Item(2)
dbconn.Close()
end sub
</script>
<!DOCTYPE html>
<html>
<body>
<form runat="server">
<asp:DataList id="customers" runat="server">
<ItemTemplate>
<%#Container.DataItem("title")%> in
<%#Container.DataItem("father")%> with
<%#Container.DataItem("age")%> years
</ItemTemplate>
</asp:DataList>
</form>
</body></html>

asp.net <form> tag getting stripped out of asp:literal

Okay... I know having nested tags is not officially supported. But stay with me on this one..
I have an ASP.NET web page with the standard <form runat=server> tag at the top. I am pulling in a Form (and associated fields) from a 3rd party source via HttpWebRequest on the server side (code behind). I can verify the data I get contains a <form> tag -- via a Trace statement. Then I assign the data to my literal like this:
Dim objRequest As System.Net.HttpWebRequest = System.Net.WebRequest.Create(url)
Dim objResponse As System.Net.WebResponse
objRequest.Method = "POST"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData.ToString)
objRequest.ContentType = "application/x-www-form-urlencoded"
objRequest.ContentLength = byteArray.Length
Dim dataStream As Stream = objRequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
objResponse = objRequest.GetResponse()
Dim streamRead As New StreamReader(objResponse.GetResponseStream())
Dim strResponse As String = streamRead.ReadToEnd()
me.litCMSForm.Text = strResponse
When the page is rendered, somehow .NET removed the <form> tag that was within the literal.
I also tried assigning the variable "strResponse" to a public variable to be displayed and it too had the tag stripped out. And I tried assigning the variable to an asp:Label as well with no luck. And I tried assigning the value to the literal on the "PreRender" function with no success.
Any thoughts on this and why .NET is stripping out the <form> tag?
Thanks!
You're correct - nested form tags are not supported. What you can do is generate JavaScript that would pass the string to an HTML container outside of ASP.NET form or, better yet, output it to an independent iframe.
I had the same problem. I was able to work around the situation by adding another form before the one I really wanted to insert. It seems like ASP was only frisky enough to strip out the first firm it found.
Perhaps this would work for you:
me.litCMSForm.Text = "<form name='fakeOut'></form>" + strResponse
I actually had my form code be a little fancier (I gave it an action and an hidden input). I didn't continue to test to see what could be taken away because it worked.

ASP URLDecode - Incorrect Conversion

I have a website where google pics up some links on my site and sends throught the following URL
http://www.globalpropertyonline.net/test.asp?town=gand%EDa
I have one of the URLDecode Functions I found on the net to Decode the %ED
However, this seems not to be working the correct way.
The %ED should be this : í
So when I Decode it the word should be gandía
But instead I get the following word : gand�a
On the page where google gets the link, it is displayed correctly as gandía but it prompts me in webmaster tools that the following link has a error 500 and this is because when I try to take this name and sends it to the MYSQL query it crash with error 500 but if I would have had gandía then it worked !
So, my main problem is that I am getting the %ED sent as URLEncode as it seems and I want to Decode it before I send it to my database query.
In my ASP Page I am using UTF-8 Encodeing as follows.
<%# CodePage=65001 Language="VBScript"%>
<%
Response.CodePage = 65001
Response.CharSet = "utf-8"
This is the Code I am using to Decode it but instead if getting the gandía I get this : gand�a
any help will be much apreciated
<%# CodePage=65001 Language="VBScript"%>
<%
Response.CodePage = 65001
Response.CharSet = "utf-8"
MyTown = Request("town")
response.write URLDecode(MyTown)
Function URLDecode(str)
str = Replace(str, "+", " ")
For i = 1 To Len(str)
sT = Mid(str, i, 1)
If sT = "%" Then
If i+2 < Len(str) Then
sR = sR & _
Chr(CLng("&H" & Mid(str, i+1, 2)))
i = i+2
End If
Else
sR = sR & sT
End If
Next
URLDecode = sR
End Function
%>
It looks like you have not defined the page as UTF-8 client-side. You can this by adding this inside <head></head>:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Asp ČŠŽ to CSZ not working

I get the string from input through post option an function
Replace(string, "Č", "C")
not working, becouse function doesen't recognise 'Č' char from variable( Request.Form("folder")).
I use for loop and this function to get char from string
strChar = mid(someString,i,1)
I could use
If Asc(strChar) = -15220 Then 'Č
string = string + "C"
but doesent always work. I think becouse 'Č' some how, use more than one char if using mid() function.
Is any other solution?
Try this: Put this one on your ASP code:
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=UTF-8"
Response.CodePage = 65001
Response.CharSet = "UTF-8"
And in your HTML code:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

Resources