HTTP_RANGE is not working on video seek bar - vbscript

I am trying to stream a mp4 file using ASP/VBscript. I use ADODB.Stream to lunch the file and some codes to detect that which range is requested by user. The video is being loaded successfully but by clicking on seek bar nothing happens. I have set all Accept-Ranges , Content-Range and Content-Length in response headers:
HTML code:
<video controls>
<source src="video.asp">
</video>
video.asp
filename ="test.mp4"
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile(server.mappath(filename))
sizebyte=objStream.size
Response.ContentType = "video/mp4"
Response.AddHeader "Accept-Ranges", "0-" & sizebyte
'Checking if user requested a range (by clicking on seekbar)
if Request.ServerVariables("HTTP_RANGE")<>"" then
'Cleaning startbyte from (bytes=) and (-)
startbyte=replace(Request.ServerVariables("HTTP_RANGE"),"-","")
startbyte=replace(startbyte,"bytes=","")
Response.AddHeader "Content-Range", "bytes " & startbyte & "-" & sizebyte-1 & "/" & sizebyte
response.AddHeader "Content-Length", (int(sizebyte) - int(startbyte))
else
response.AddHeader "Content-Length", sizebyte
end if
do while not objStream.EOS
response.binarywrite objStream.Read(1024000)
Response.Flush
loop
objStream.Close
Set objStream = Nothing
Response.StatusCode = 206

Related

How can I send http command in vbs [duplicate]

Is there a way to perform an HTTP GET request within a Visual Basic script? I need to get the contents of the response from a particular URL for processing.
Dim o
Set o = CreateObject("MSXML2.XMLHTTP")
o.open "GET", "http://www.example.com", False
o.send
' o.responseText now holds the response as a string.
You haven't at time of writing described what you are going to do with the response or what its content type is. An answer already contains a very basic usage of MSXML2.XMLHTTP (I recommend the more explicit MSXML2.XMLHTTP.3.0 progID) however you may need to do different things with the response, it may not be text.
The XMLHTTP also has a responseBody property which is a byte array version of the reponse and there is a responseStream which is an IStream wrapper for the response.
Note that in a server-side requirement (e.g., VBScript hosted in ASP) you would use MSXML.ServerXMLHTTP.3.0 or WinHttp.WinHttpRequest.5.1 (which has a near identical interface).
Here is an example of using XmlHttp to fetch a PDF file and store it:-
Dim oXMLHTTP
Dim oStream
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
oXMLHTTP.Open "GET", "http://someserver/folder/file.pdf", False
oXMLHTTP.Send
If oXMLHTTP.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write oXMLHTTP.responseBody
oStream.SaveToFile "c:\somefolder\file.pdf"
oStream.Close
End If
If you are using the GET request to actually SEND data...
check:
http://techhelplist.com/index.php/tech-tutorials/37-windows-troubles/60-vbscript-sending-get-request
The problem with MSXML2.XMLHTTP is that there are several versions of it, with different names depending on the windows os version and patches.
this explains it:
http://support.microsoft.com/kb/269238
i have had more luck using vbscript to call
set ID = CreateObject("InternetExplorer.Application")
IE.visible = 0
IE.navigate "http://example.com/parser.php?key=" & value & "key2=" & value2
do while IE.Busy....
....and more stuff but just to let the request go thru.
strRequest = "<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" " &_
"xmlns:tem=""http://tempuri.org/"">" &_
"<soap:Header/>" &_
"<soap:Body>" &_
"<tem:Authorization>" &_
"<tem:strCC>"&1234123412341234&"</tem:strCC>" &_
"<tem:strEXPMNTH>"&11&"</tem:strEXPMNTH>" &_
"<tem:CVV2>"&123&"</tem:CVV2>" &_
"<tem:strYR>"&23&"</tem:strYR>" &_
"<tem:dblAmount>"&1235&"</tem:dblAmount>" &_
"</tem:Authorization>" &_
"</soap:Body>" &_
"</soap:Envelope>"
EndPointLink = "http://www.trainingrite.net/trainingrite_epaysystem" &_
"/trainingrite_epaysystem/tr_epaysys.asmx"
dim http
set http=createObject("Microsoft.XMLHTTP")
http.open "POST",EndPointLink,false
http.setRequestHeader "Content-Type","text/xml"
msgbox "REQUEST : " & strRequest
http.send strRequest
If http.Status = 200 Then
'msgbox "RESPONSE : " & http.responseXML.xml
msgbox "RESPONSE : " & http.responseText
responseText=http.responseText
else
msgbox "ERRCODE : " & http.status
End If
Call ParseTag(responseText,"AuthorizationResult")
Call CreateXMLEvidence(responseText,strRequest)
'Function to fetch the required message from a TAG
Function ParseTag(ResponseXML,SearchTag)
ResponseMessage=split(split(split(ResponseXML,SearchTag)(1),"</")(0),">")(1)
Msgbox ResponseMessage
End Function
'Function to create XML test evidence files
Function CreateXMLEvidence(ResponseXML,strRequest)
Set fso=createobject("Scripting.FileSystemObject")
Set qfile=fso.CreateTextFile("C:\Users\RajkumarJoshua\Desktop\DCIM\SampleResponse.xml",2)
Set qfile1=fso.CreateTextFile("C:\Users\RajkumarJoshua\Desktop\DCIM\SampleReuest.xml",2)
qfile.write ResponseXML
qfile.close
qfile1.write strRequest
qfile1.close
End Function

Download files using classic asp

I followed the below link but I am not able to download the file :
StackOverflow Question on same Topic from where I used the code
Here is my download.asp code :
<%
Dim objConn, strFile
Dim intCampaignRecipientID
strFile = Request.QueryString("file")
If strFile <> "" Then
Response.Buffer = False
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1 'adTypeBinary
objStream.Open
'FilePath=server.MapPath("some_folder/DOWNLOAD/")
'Response.Write(Server.MapPath("some_folder/download/") & "\" & strFile)
objStream.LoadFromFile(Server.MapPath("some_folder/download/") & "\" &
strFile)
Response.ContentType = "application/x-unknown"
Response.Addheader "Content-Disposition", "attachment; filename=" & strFile
Response.BinaryWrite objStream.Read
objStream.Close
Set objStream = Nothing
End If
%>
I am getting Error Invalid Response...The page is not accessible error.
The download folder is at the same level as wwwroot folder so the folder structure is :
wwwroot some_folder/download
I have my download folder in wwwroot/download.asp that is why I am using server.mappath. I even tried ../some_folder/download but same error.
Can someone help me in solving this issue.
Thanks in Advance.

Downloading large files using XML HTTP Request

I'm trying to make a simple launcher for a game that can install and uninstall mods, and update them. I don't know a ton about VBScript, so I got most of the downloading/update code off of the internet. I have most of the code done, but I have a problem. I'm trying to download about a 1/2 GB large zip folder off of my google drive. It was working when all I had was a simple text file in the zip folder as a test, but all it did was download a corrupted zip folder. WinRar can't open it, giving the error message "The archive is in either unknown format or damaged". So my question is does the code that I'm using have a limit to the file size? I'm trying to download https://drive.google.com/uc?export=download&id=0BxlXlAM9nwYTZTFUdXpWQlJyN2M which is a direct download link to my google drive file.
Dim http: Set http = createobject("Microsoft.XMLHTTP")
Dim stream: Set stream = createobject("Adodb.Stream")
http.Open "GET", "https://drive.google.com/uc?export=download&id=0BxlXlAM9nwYTZTFUdXpWQlJyN2M", False
http.Send
With stream
.Type = 1
.Open
.Write http.responseBody
.SaveToFile "c:\updates\normaldata\normal.zip", 2
End With
As #Noodles already pointed out your original link is being redirected to a confirmation page, because the file is too large to be scanned for malware. You need to extract the actual download link from that page, e.g. like this:
baseUrl = "https://drive.google.com"
url = baseUrl & "/uc?export=download&id=0BxlXlAM9nwYTZTFUdXpWQlJyN2M"
Set http = CreateObject("Microsoft.XMLHTTP")
http.Open "GET", url, False
http.Send
If http.Status <> 200 Then
WScript.Echo http.Status & " " & http.StatusText
WScript.Quit 1
ElseIf Left(http.getResponseHeader("Content-Type"), 9) = "text/html" Then
Set html = CreateObject("HTMLFile")
html.Write http.ResponseText
Set dl = html.GetElementById("uc-download-link")
http.Open "GET", Replace(dl.href, "about:", baseUrl), False
http.Send
...
End If
However, when I tried to follow the link I got an access denied error, whereas the download worked fine in a browser. To make the VBScript work you may need to inspect the conversation between browser and the webserver (e.g. with Fiddler) and adjust the second HTTP request according to the results.
First that link is not a direct download. It takes you to a page that warns you the zip can't be virus scanned by Google.
What you aren't doing is
Opening the file in notepad and seeing what it says. Zips start with PK, Executables and dlls with MZ, and you'll recognise HTML. You are downloading a web page that is under 1000 bytes.
You have no error checking. Note my code is 80% error checking.
#define HTTP_STATUS_OK 200 // request completed
It gave you the page it wanted to give you.
This downloads a web page and saves it as a zip. Rename it html and open in internet explorer.
Set fso = CreateObject("Scripting.FileSystemObject")
Set Outp = Wscript.Stdout
Set wshShell = CreateObject("Wscript.Shell")
Set ShApp = CreateObject("Shell.Application")
On Error Resume Next
Set File = WScript.CreateObject("Msxml2.XMLHTTP.6.0")
File.Open "GET", "https://drive.google.com/uc?export=download&id=0BxlXlAM9nwYTZTFUdXpWQlJyN2M", False
File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)"
File.Send
If err.number <> 0 then
wscript.echo ""
wscript.echo "Error getting file"
wscript.echo "=================="
wscript.echo ""
wscript.echo "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description
wscript.echo "Source " & err.source
wscript.echo ""
wscript.echo "HTTP Error " & File.Status & " " & File.StatusText
wscript.echo File.getAllResponseHeaders
wscript.echo File.ResponseBody
else
On Error Goto 0
wscript.echo "Server Response " & File.Status & " " & File.StatusText
wscript.echo File.getAllResponseHeaders
wscript.echo File.ResponseBody
Set BS = CreateObject("ADODB.Stream")
BS.type = 1
BS.open
BS.Write File.ResponseBody
BS.SaveToFile ShApp.Namespace(&h10).self.path & "\file.zip", 2
End If

Classic ASP download PDF on page load

I want to create a page that can display a message "Your download is about to begin" and then after a couple seconds open a "save as" dialogue which allows the visitor to download a file. Is this possible in Classic ASP VB Script? I know how to make a page stream a file, but it doesn't show the html of the page. The file I am offering is 20Mb so the script needs to handle large files sizes.
I currently have a meta redirect in place:
<meta http-equiv="refresh" content="2; url=/downloads/brochures/ACET_Products_and_Services_Directory_2013-14.pdf" />
But this isn't really any good.
I have asppdf installed on my server, and gave this a go:
<%
Set Pdf = Server.CreateObject("Persits.Pdf")
Set Doc = Pdf.OpenDocument("d:/websites/common/downloads/brochures/ACET_Products_and_Services_Directory_2013-14.pdf")
Doc.SaveHttp "attachment;filename=ACET_Products_and_Services_Directory_2013-14.pdf"
%>
This gets around the large file, but you can't stream the file and display HTML at the same time.
I have found plenty of ways to stream the file to the browser, but I can't it to do this after the page has been displayed.
This is another one I have tried:
<%
Response.Buffer = False
Server.ScriptTimeout = 30000
Response.ContentType = "application/x-unknown" ' arbitrary
fn = "ACET_Products_and_Services_Directory_2013-14.pdf"
FPath = "d:\websites\common\downloads\brochures\" & fn
Response.AddHeader "Content-Disposition", "attachment; filename=" & fn
Set adoStream = CreateObject("ADODB.Stream")
chunk = 2048
adoStream.Open()
adoStream.Type = 1
adoStream.LoadFromFile(FPath)
iSz = adoStream.Size
Response.AddHeader "Content-Length", iSz
For i = 1 To iSz \ chunk
If Not Response.IsClientConnected Then Exit For
Response.BinaryWrite adoStream.Read(chunk)
Next
If iSz Mod chunk > 0 Then
If Response.IsClientConnected Then
Response.BinaryWrite adoStream.Read(iSz Mod chunk)
End If
End If
adoStream.Close
Set adoStream = Nothing
Response.End
%>
With this I get a Error code: ERR_INVALID_RESPONSE from Chrome.
This is one that I have tried that almost works:
<%
strFilePath = "d:/web sites/common/downloads/brochures/ACET_Products_and_Services_Directory_2013-14.pdf"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFilePath) Then
Set objFile = objFSO.GetFile(strFilePath)
intFileSize = objFile.Size
Set objFile = Nothing
strFileName = "ACET_Products_and_Services_Directory_2013-14.pdf"
Response.AddHeader "Content-Disposition","attachment; filename=" & strFileName
Response.ContentType = "application/x-msdownload"
Response.AddHeader "Content-Length", intFileSize
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'adTypeBinary
objStream.LoadFromFile strFilePath
Do While Not objStream.EOS And Response.IsClientConnected
Response.BinaryWrite objStream.Read(1024)
Response.Flush()
Loop
objStream.Close
Set objStream = Nothing
Else
Response.write "Error finding file."
End if
Set objFSO = Nothing
%>
I then used <% response.redirect("download.asp") %> on the page I want it to download from, but as soon as I hit the page I get the file, but no page. Its this part I am struggling with.
SUCCESS!
<script>
window.location.replace('download.asp');
</script>
Cheers,
Steve
With a little more trial and error I discovered creating a file called download.asp and putting this code in worked:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
strFilePath = "d:/websites/common/downloads/brochures/ACET_Products_and_Services_Directory_2013-14.pdf"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFilePath) Then
Set objFile = objFSO.GetFile(strFilePath)
intFileSize = objFile.Size
Set objFile = Nothing
strFileName = "ACET_Products_and_Services_Directory_2013-14.pdf"
Response.AddHeader "Content-Disposition","attachment; filename=" & strFileName
Response.ContentType = "application/pdf"
Response.AddHeader "Content-Length", intFileSize
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'adTypeBinary
objStream.LoadFromFile strFilePath
Do While Not objStream.EOS And Response.IsClientConnected
Response.BinaryWrite objStream.Read(1024)
Response.Flush()
Loop
objStream.Close
Set objStream = Nothing
Else
Response.write "Error finding file."
End if
Set objFSO = Nothing
%>
I then placed this code on the page I wanted to display the instructions and then offer the automatic download:
<script>
window.location.replace('download.asp');
</script>
I hope someone else finds this useful.
Steve

Post data with https does not get processed at Server side

I am having a VBScript which gets invoked from a 3rd party application, the script gets the data from the 3rd party application and opens the URL in an IE browser by passing the data in POST format. The script works fine when I use http as the protocol but the moment I use https, the server side code (request.getParameter("param1")) complains that it is not able to find the parameter in the request object. The script is called by passing the URL and the data. e.g. run.vbs https://xyz.com?param1=1234. Following is the vbscript for your kind perusal. Can you please let me know what I am missing when I am using https as the protocol. Any help is highly appreciated. Many thanks.
If WScript.Arguments.Count = 1 Then
uri = WScript.Arguments.Item(0)
'WScript.Echo "Arguments " & uri
Set WshNetwork = WScript.CreateObject("WScript.Network")
'WScript.Echo "Current User Name: " & WshNetwork.UserName
filename="C:\Documents and Settings\"+WshNetwork.UserName+"\Application Data\XYZ\Profiles\default\Settings.xml"
'WScript.Echo "Current User fileName: " & filename
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load(filename)
strQuery = "Agent"
Set colItem = xmlDoc.getElementsByTagname(strQuery)
For Each objItem in colItem
Agentid = objItem.getAttribute("Login")
'MsgBox "AgentId = " + AgentID
Next
'uri = uri+"^&agentid="+Agentid
uri = uri+"&agentid="+Agentid
pos = InStr(uri,"?")
extracteduri = Mid(uri,1,pos-1)
params = Mid(uri, pos+1)
postdata = Str2Bytes(params,"us-ascii")
header = "Content-Type: application/x-www-form-urlencoded"
Set IE = CreateObject("InternetExplorer.Application")
'IE.Navigate "about:blank"
'IE.AddressBar = True
'IE.ToolBar = True
'IE.StatusBar = True
IE.Visible = True
'WScript.Sleep 2000
Set shl = WScript.CreateObject("WScript.Shell")
shl.SendKeys "% X"
IE.Navigate extracteduri, Nothing, Nothing, postdata, header
Wscript.Quit
Else
Wscript.Echo "Usage: RunURL.vbs <URL to invoke>"
Wscript.Quit
End If
Function Str2Bytes(Text, CharSet)
Const adTypeText = 2
Const adTypeBinary = 1
'Create Stream object
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
If Len(CharSet) > 0 Then
BinaryStream.CharSet = CharSet
Else
BinaryStream.CharSet = "us-ascii"
End If
'Open the stream And write text/string data To the object
BinaryStream.Open
BinaryStream.WriteText Text
'Change stream type To binary
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary
'Ignore first two bytes - sign of
BinaryStream.Position = 0
'Open the stream And get binary data from the object
Str2Bytes = BinaryStream.Read
End Function
I am now using the below function to get the response from the server using https as the protocol and POST as the format but still the server is not able to see the parameter
Set req = CreateObject("MSXML2.ServerXMLHTTP")
'Set the below option to get rid of the "Certificate authority is invalid or
'incorrect, error code - 80072F0D" error
req.setOption 2, 13056
req.open "POST", extracteduri, False
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
encodedParams = Escape(params)
WScript.Echo "encodedParams: " & encodedParams
req.send encodedParams
WScript.Echo "req.responseText: " & req.responseText
Below are the encoded parameters
uui%3DU1%3D123456%26agentid%3D123456
The server still complains that the parameter is missing from the request object.
I am using the same script (XMLHTTP request) but I am encrypting the parameters using the Str2Bytes function (declared above)
Set req = CreateObject("MSXML2.ServerXMLHTTP")
req.setOption 2, 13056
req.open "POST", extracteduri, False
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send postdata
WScript.Echo "req.responseText: " & req.responseText
I'd recommend using an XMLHTTPRequest instead of the Internet Explorer COM object. See here for an example. Note that you must encode special characters in the post data.
However, if you want to open the URL in IE anyway, you'd probably better stick with your original approach. In that case I'd check the server for more information about why the server side thinks it cannot find the parameter. Does the request even have a parameter named "param1"?

Resources