ASP URLDecode - Incorrect Conversion - asp-classic

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" />

Related

Callback URL is ignoring parameter value after &

I got stuck at this point. After login i am getting referral-url which i am putting into www.url.com?par1=val&callback="referral-url". My referral url is like www.ref-url.com?param1=val1&param2=val2&param3=val3. My problem is that i am getting a cut url i.e., www.ref-url.com?param1=val1 after login. I think it is ignoring url after '&'.I am using classic asp for development. Any Help would be very helpful.
You need to use Server.URLEncode if you're including a URL as a querystring parameter, especially if the included URL also contains querystrings.
Dim login_redirect, login_referrer
login_redirect = "http://www.url.com/?par1=val&callback="
login_referrer = "http://www.ref-url.com/?param1=val1&param2=val2&param3=val3"
response.write login_redirect & Server.URLEncode(login_referrer)
Output:
http://www.url.com/?par1=val&callback=http%3A%2F%2Fwww%2Eref%2Durl%2Ecom%2F%3Fparam1%3Dval1%26param2%3Dval2%26param3%3Dval3
Passing the URL with query inside another URL query is a bit tricky. The only way it works is to encode it. For example:
https://website.com/?a=1&url=https%3A%2F%2Fwebsite.com%2F%3Fz%3D1%26y%3D2
But, when you want to return to the url you passed through query, you need to decode it otherwise it will not work. You can use the following function on your "login Page" before redirecting the url.
Function URLDecode(sConvert)
Dim aSplit
Dim sOutput
Dim I
If IsNull(sConvert) Then
URLDecode = ""
Exit Function
End If
' convert all pluses to spaces
sOutput = REPLACE(sConvert, "+", " ")
' next convert %hexdigits to the character
aSplit = Split(sOutput, "%")
If IsArray(aSplit) Then
sOutput = aSplit(0)
For I = 0 to UBound(aSplit) - 1
sOutput = sOutput & _
Chr("&H" & Left(aSplit(i + 1), 2)) &_
Right(aSplit(i + 1), Len(aSplit(i + 1)) - 2)
Next
End If
URLDecode = sOutput
End Function
For example, you should have above function and the following code on your login page:
Dim callback
callback = Request("callback")
callback = URLDecode(callback)
Response.redirect(callback)

MSXML2.ServerXMLHTTP using Classic ASP returns broken images

I haven't used MSXML2.ServerXMLHTTP in years and now i need to. When i use MSXML2.ServerXMLHTTP to grab a page, the page returns with broken images. I remember doing this in the past, there was a line of code i would use and the images would resolve perfectly. It was sort of like setting the base url. Does anyone know what the code would be? Here's the code i'm using:
url = "notimportant.com"
Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
objXML.Open "GET", URL, False
objXML.Send()
xmlResponse = objXML.responseText
Set objXML = Nothing
You probably want to place a <base> tag inside the <head> so that one line of code must be the following:
xmlResponse = Replace(objXML.responseText, "<head>", "<head><base href=""http://notimportant.com/"" />", 1, 1, vbTextCompare)
Or as a more reliable way in case where the head tag is more complex and unpredictable like <head class="head etc">, you can use regular expressions to replace:
Dim Re
Set Re = New RegExp
Re.IgnoreCase = True
Re.Pattern = "<head[^>]*>"
xmlResponse = Re.Replace(objXML.responseText, "$&<base href=""http://notimportant.com/"" />")

Class ASP - Response.ContentType Conflicts with Progress Bar

I am having problem with Response.ContentType Conflicts with Progress Bar. Basically, I am trying to display a pop up progress bar while saving the recordset to a CSV file.
I use this two lines below everywhere to call the progress bar and close it at the bottom of a page, so it works with regular page without (Response.AddHeader "Content-Disposition", "attachment;filename=export.csv")
response.write("<script language=""javascript"">ProgressStart();</script>")
Response.Write("<script>ProgressDestroy()</script>")
I am getting this error below, and I know it is conflicting with the Response.ContactType "text/csv".
Response object error 'ASP 0156 : 80004005'
Header Error
/Apps/ERP/Company/ExportCompanyView.asp, line 253
The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
Is there a way to by pass or trick it, so I won't conflict with "Response.ContentType = "text/csv""?
Thanks in advance,
This code is where all the process happens:
If SOViewSQL <> "" Then
set rs1 = conn.execute(SOViewSQL)
response.write("<script language=""javascript"">ProgressStart();</script>")
response.flush()
Write_CSV_From_Recordset RS1
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "attachment;filename=export.csv"
Response.Write("<script>ProgressDestroy()</script>")
'response.flush()
set rs1 = nothing
conn.close
set conn = nothing
End If
You'll have to rewrite your page script so all processing is done before anything (headers or page content) is sent to the client, and then make sure you send all headers before you call Response.Write or use %>.
Like so:
Dim showProgressScripts
showProgressScripts = False
If SOViewSQL <> "" Then
Set rs1 = conn.Execute(SOViewSQL)
Write_CSV_From_Recordset RS1
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "attachment;filename=export.csv"
Set rs1 = Nothing
conn.close
Set conn = Nothing
showProgressScripts = True
End If %>
<p>my page content here</p>
<% If showProgressScripts %>
<script>ProgressStart();</script>
<script>ProgressDestroy();</script>
<% End If %>
Of course, your script looks like you're serving HTML as a CSV file - I think you need to reconsider what you're doing.

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" />

ASP.NET - remote screenshot

I made a very very simple small app to take screenshot of the desktop and send to network share. About 10 PC's would have this app installed.
My idea is, that there will be one dashboard in ASP.NET, which simply shows those screenshots on the webpage. So far, easy stuff.
But, because I don't want to clog the network and send the screenshot every 1 minute, I would like to launch the .exe on the remote PC's by demand of ASP.NET user.
Unfortunately I haven't found any information (and I'm a complete ASP.NET n00b), how to launch remote executable IN the context of the remote PC (so I won't see screenshots of ASP server :) )
If there is no such possibility, please advise about other way to solve this.
Update after clarification:
Take a look at the situation from another angle:
Why don't you run a web server on the clients that host an asp.net page that triggers the capture. Then you can, from your root server, simply sent http requests to the clients and fetch the image.
You can try http://CassiniDev.codeplex.com - it supports external IP and hostnames.
And you may also consider simply embedding the CassiniDev-lib (a very simple example is shown here - Using CassiniDev to host ASP.Net in your application, that way you can use the web server as the reciever and the forms app can do whatever it wants on the client.
I am confident in this approach as I designed cassinidev with this as one of the primary use cases.
From asp.net you cannot. It is only HTML/JavaScript once it gets to the browser.
ActiveX is a possibility but it is quite painful and dated and limited. And painful.
The new way to do something like this is to deploy a .net Forms application or WPF app via Click Once.
You can also write a WPF Browser Application but getting the kind of permissions you would need would entail setting the site as full trust.
If a web page could launch an arbitrary .exe file on your machine, that would be a security disaster.
However, since these are your PCs, you can require them to install an ActiveX control of some kind that you could then embed in your ASP.NET page.
As others have said, there is really no way for ASP.Net to call out to the apps, but reversing the control flow should work OK...
I suppose you could have the grabber application running all the time on the users desktop, but have it make a call to a web service / file served by the server that contains an instruction for that instance of the app to grab a screenshot.
Something like...
App : Do I have to do anything? (GET /workinstruction.aspx)
Server : no. (server decides whether to request work, and return the result in (workinstruction.aspx)
App : (waits 1 minute)
App : Do I have to do anything?
Server : yes.
App : (takes screenshot and submits)
App : (waits 1 minute)
App : Do I have to do anything?
etc...
Thank you all for answering, those were interesting approaches to the subject.
Yet due to many factors I ended up with following solution:
Pseudo-service (Windows Forms with tray icon and hidden form) application on client PC's. It is serving as TCP server.
ASP.Net web app on the server, with TCP client function.
On request of the web user, web app is sending preformatted TCP 'activation' string to the chosen PC. Tray app is making a screenshot and sending it to predefined SMB share, available for web app to display.
Thanks again!
I've done this exact thing a few times for monitoring remote display systems. What I found was that using MiniCap.exe to capture image also took video (which was required on remote display systems).
I also used Cassini as described by Sky Sanders with an ASPX-page with the following code.
Then I just reference the page from an img src="http://computer/page.aspx?paramters". (Let me know if you need more info)
<%# Import NameSpace="System.IO" %>
<%# Import NameSpace="System.Drawing" %>
<%# Import NameSpace="System.Drawing.Imaging" %>
<%# Import NameSpace="System.Diagnostics" %>
<%
Response.Buffer = True
Response.BufferOutput = True
Dim CompressionLevel As Integer = 1
Dim compress As Integer = 1
If Not Request.Item("compress") Is Nothing Then
If IsNumeric(Request.Item("compress")) = True Then
CompressionLevel = CInt(Request.Item("compress"))
End If
End If
compress = CompressionLevel
' Resize requested?
Dim SizeX As Integer = 100
Dim SizeY As Integer = 75
If Not Request.Item("width") Is Nothing Then
If IsNumeric(Request.Item("width")) = True Then
SizeX = CInt(Request.Item("width"))
CompressionLevel = 10
End If
End If
If Not Request.Item("height") Is Nothing Then
If IsNumeric(Request.Item("height")) = True Then
SizeY = CInt(Request.Item("height"))
CompressionLevel = 10
End If
End If
Dim Region As String = ""
If Not Request.Item("region") Is Nothing Then
Region = Request.Item("region")
End If
Dim XS As Integer = 0
Dim YS As Integer = 0
Dim XE As Integer = 1023
Dim YE As Integer = 766
Try
If Region.IndexOf(",") > -1 Then
Dim Rec() As String = Region.Split(",")
If Rec.GetUpperBound(0) >= 3 Then
If IsNumeric(Rec(0)) Then XS = Rec(0)
If IsNumeric(Rec(1)) Then YS = Rec(1)
If IsNumeric(Rec(2)) Then XE = Rec(2)
If IsNumeric(Rec(3)) Then YE = Rec(3)
End If
End If
Catch : End Try
Dim FileType As String = "jpg"
Dim MimeType As String = "jpeg"
If Not Request.Item("filetype") Is Nothing Then
FileType = Request.Item("filetype")
MimeType = FileType
End If
If Not Request.Item("mimetype") Is Nothing Then
FileType = Request.Item("mimetype")
End If
Dim ImageFile As String = ""
Dim ImageThumbFile As String = ""
Dim ImageFolder As String = Server.MapPath("~/ScreenShots/")
If IO.Directory.Exists(ImageFolder) = False Then
IO.Directory.CreateDirectory(ImageFolder)
End If
' Delete files older than 30 minutes
For Each File As String In IO.Directory.GetFiles(ImageFolder)
Response.Write("File: " & File & "<br>")
If IO.File.GetCreationTimeUtc(File).AddMinutes(30) < Now.ToUniversalTime Then
IO.File.Delete(File)
End If
Next
' Find available filename
Dim tmpC As Integer = 0
While tmpC < 100
tmpC += 1
ImageFile = "ScreenShot_" & CStr(tmpC).PadLeft(5, "0") & "." & FileType
ImageThumbFile = "ScreenShot_" & CStr(tmpC).PadLeft(5, "0") & "_thumb." & FileType
If IO.File.Exists(ImageFolder & "\" & ImageFile) = False Then
' Found our filename
' Reserve it
Dim ios As IO.FileStream = IO.File.Create(ImageFolder & "\" & ImageFile)
ios.Close()
ios = Nothing
Exit While
End If
End While
' Run MiniCap
' " -capturedesktop" & _
Dim CMD As String = """" & Server.MapPath("/MiniCap.EXE") & """" & _
" -save """ & ImageFolder & "\" & ImageFile & """" & _
" -captureregion " & XS & " " & YS & " " & XE & " " & YE & _
" -exit" & _
" -compress " & CompressionLevel
If Not CMD Is Nothing Then
Dim myProcess As Process = New Process
Dim RouteFB As String
With myProcess
With .StartInfo
.FileName = "cmd.exe"
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
End With
.Start()
End With
Dim sIn As IO.StreamWriter = myProcess.StandardInput
sIn.AutoFlush = True
' Create stream reader/writer references
Dim sOut As IO.StreamReader = myProcess.StandardOutput
Dim sErr As IO.StreamReader = myProcess.StandardError
' Send commands
sIn.Write(CMD & System.Environment.NewLine)
sIn.Write("exit" & System.Environment.NewLine)
' Wait one second
'Threading.Thread.CurrentThread.Sleep(60000)
' Read all data
Response.Write(sOut.ReadToEnd)
' Kill process if still running
If Not myProcess.HasExited Then
myProcess.Kill()
End If
sIn.Close()
sOut.Close()
sErr.Close()
myProcess.Close()
End If
Response.Clear()
Response.ClearContent()
If Not Request.Item("width") Is Nothing Or Not Request.Item("length") Is Nothing Then
' Resize, making thumbnail in desired size
Dim b As Bitmap = Bitmap.FromFile(ImageFolder & "\" & ImageFile)
Dim thumb As Bitmap = b.GetThumbnailImage(SizeX, SizeY, Nothing, IntPtr.Zero)
' Jpeg image codec
Dim jpegCodec As ImageCodecInfo
' Get image codecs for all image formats
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
' Find the correct image codec
For i As Integer = 0 To codecs.Length - 1
If (codecs(i).MimeType = "image/" & MimeType) Then
jpegCodec = codecs(i)
Exit For
End If
Next i
Dim qualityParam As New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compress * 10)
Dim encoderParams As New EncoderParameters(1)
encoderParams.Param(0) = qualityParam
thumb.Save(ImageFolder & "\" & ImageThumbFile, jpegCodec, encoderParams)
thumb.Dispose()
b.Dispose()
' Send thumb
Response.TransmitFile(ImageFolder & "\" & ImageThumbFile)
Else
' Send normal file
Response.TransmitFile(ImageFolder & "\" & ImageFile)
End If
Response.End()
%>

Resources