XMLHTTP or ServerXMLHTTP working without http? - http

I want to open a webpage and after that split the tags, without starting the url with 'www' not 'http'
see image below
<%
dim objXMLHTTP
' holds a warning when the page seems to be invalid
URL = Request.form("URL")
if ( URL = "" ) then
**'URL = "www.elmundo.es"**
URL = Request.QueryString("url")
end if
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.Open "GET", URL, false
objXMLHTTP.Send
%>

Related

read all elements from another page using classic ASP

Is there a way to read a another page's elements and grab the whole HTML code?
let's say for example i want to get all the HTML from this page : http://localhost/test3.html
Thank you!
Here's a function I use:
function http_post (strUrl, data)
' call another URL (e.g. ASP or PHP script) via HTTP and POST. "data" should be GET-style parameter string or empty '
dim xmlHttp
Set xmlHttp = Server.Createobject("MSXML2.ServerXMLHTTP.6.0")
With xmlHttp
.Open "POST", strUrl, False
.setRequestHeader "User-Agent", Request.ServerVariables("HTTP_USER_AGENT")
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Content-Length", Len(data)
.Send data
if .Status < 400 then
http_post = .responseText
else
' do error logging here if you want '
http_post = false
end if
.abort()
End With
set xmlHttp = Nothing
end function

Excel vba download file with random file extension

I am trying to write a code to automatically download files from a website by Excel vba. I know there are plenty of posts regarding this topic but no luck so far. The first few lines of code go like this:
Sub testing()
Dim ie as object
Url _base = "http://www..../download.aspx?id="
Num = cells(1,1).value
Set ie = createobject ("internetexplorer.application")
Ie.visible = true
For i = 1 to num
Url = url _base & i
....
Then I become clueless. The problem is that winhttp seems to only download csv files, and urldownloadtofile requires a solid url path ending with the file extension. However, my case is the link is redirecting to the actual file location (no extension shown), and also the file could be any extension such as pdf, jpg, and doc.
Thanks all in advance!
Ok, editing answer to fold in feedback, three different ways to make an HTTP request and it seems you are looking to trap a redirect which is status codes 300-303, 307-308. Try this and provide feedback as to whether or not you are redirected.
Option Explicit
Private Sub TestGetFileFromWeb()
Call SaveTextToFile(GetFileFromWeb2("http://www.wikipedia.com"), "c:\temp\wikipedia2.txt")
Call SaveTextToFile(GetFileFromWeb3("http://www.wikipedia.com"), "c:\temp\wikipedia3.txt")
'* placed last because it gives "Access Denied" Run-time error '-2147024891 &h80070005
'Call SaveTextToFile(GetFileFromWeb1("http://www.wikipedia.com"), "c:\temp\wikipedia1.txt")
Call SaveTextToFile(GetFileFromWeb1("http://www.bbc.com"), "c:\temp\bbc.txt")
End Sub
Private Function SaveTextToFile(ByRef sText As String, ByVal sFileName As String) As Boolean
'* Requires Tools ->References -> Microsoft Scripting Runtime
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim txtOut As Scripting.TextStream
Set txtOut = fso.CreateTextFile(sFileName, , True)
txtOut.Write sText
txtOut.Close
Set txtOut = Nothing
Set fso = Nothing
SaveTextToFile = True
End Function
Private Function GetFileFromWeb1(ByVal sURL As String) As String
'* Requires Tools->References->Microsoft Xml, v.6.0
Dim xHTTPRequest As MSXML2.XMLHTTP60
Set xHTTPRequest = New MSXML2.XMLHTTP60
xHTTPRequest.Open "GET", sURL, False
xHTTPRequest.Send
Debug.Assert WasRedirected(xHTTPRequest.Status)
GetFileFromWeb1 = xHTTPRequest.ResponseText
End Function
Private Function GetFileFromWeb2(ByVal sURL As String) As String
'* Requires Tools->References->Microsoft WinHTTP Services, version 5.1
Dim oWinHttp As WinHttp.WinHttpRequest
Set oWinHttp = New WinHttp.WinHttpRequest
oWinHttp.Open "GET", sURL, False
oWinHttp.Send
Debug.Assert WasRedirected(oWinHttp.Status)
GetFileFromWeb2 = oWinHttp.ResponseText
End Function
Private Function WasRedirected(ByVal lStatus As Long) As Boolean
'http://qnimate.com/redirection-and-duplicate-content-in-websites/
'There are many types of HTTP redirection.
'
'300 Redirect or Multiple Choices
'301 Redirect or permanent redirect
'302 Redirect or Found or Temporary Redirect
'303 Redirect or See Other
'307 Redirect or Temporary Redirect
'308 Redirect or Permanent Redirect
'HTTP refresh header
WasRedirected = (lStatus = 300 Or lStatus = 301 Or lStatus = 302 Or lStatus = 303 Or lStatus = 307 Or lStatus = 308)
End Function
Private Function GetFileFromWeb3(ByVal sURL As String) As String
'* Requires Tools->References->Microsoft Xml, v.6.0
Dim xHTTPRequest As MSXML2.ServerXMLHTTP60
Set xHTTPRequest = New MSXML2.ServerXMLHTTP60
xHTTPRequest.Open "GET", sURL, False
xHTTPRequest.Send
Debug.Assert WasRedirected(xHTTPRequest.Status)
GetFileFromWeb3 = xHTTPRequest.ResponseText
End Function

How to execute a URL without running in webbrowser on button click?

After clicking a button in ASPX page I have to execute a URL
http://127.0.0.1/phptest/sendmail.php?id=+#USERNAME
using VB.net. #USERNAME comes from textbox.
Make an HTTP request if you need to call a URL from VBScript:
username = ...
url = "http://127.0.0.1/phptest/sendmail.php?id=+#" & username
Set req = CreateObject("Msxml2.XMLHttp.6.0")
req.open "GET", url, False
req.send
If req.status = 200 Then
'request successful
Else
'request failed
End If
Try something like that in vbscript :
Dim URL,ws,USERNAME
USERNAME = InputBox("Type your USERNAME","USERNAME","USERNAME")
URL = "http://127.0.0.1/phptest/sendmail.php?id=+#"& USERNAME &""
set ws = CreateObject("wscript.shell")
ws.run URL

classic asp xml receiver page doesn't receive the xml (or so it seems)

I am working on Classic ASP and was going through a possible solution of posting and reading an xml.
I read Tim's reply (which is pasted below) but this doesn't work for me. It seems xmlDoc.load(Request) doesn't load anything. What could be wrong here? I would very much appreciate a prompt response.
This is the posting page:
url = "www.receivingwebsite.com\asp\receivingwebpage.asp"
information = "<Send><UserName>Colt</UserName><PassWord>Taylor</PassWord><Data>100</Data></Send>"
Set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST", url, false
xmlhttp.setRequestHeader "Content-Type", "text/xml"
xmlhttp.send information
This is the receiving page:
Dim xmlDoc
Dim userName
set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(Request)
userName = xmlDoc.documentElement.selectSingleNode("UserName").firstChild.nodeValue
Try this :
Dim objXmlRequest
Set objXmlRequest = Server.CreateObject("MSXML2.DOMDOCUMENT.3.0")
objXmlRequest.async = False
objXmlRequest.setProperty "ServerHTTPRequest", True
objXmlRequest.validateOnParse = True
objXmlRequest.preserveWhiteSpace = False
IF objXmlRequest.Load (Request) THEN
''' GET THE REQUEST FROM CLIENT
strQuery = "//" & "ActionName"
Set oNode = objXmlRequest.selectSingleNode(strQuery)
strActionName = oNode.Text
END IF
' The key is in the property set ... check ".setProperty "ServerHTTPRequest", True"
Bye, Martin.

msxml domdocument stops working when I move to staging server

I have some code that looks like this:
Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlHttp.Open "Get", myRSSfile, false
xmlHttp.Send()
myXML = xmlHttp.ResponseText
Set xmlResponse = Server.CreateObject("MSXML2.DomDocument")
xmlResponse.async = false
xmlResponse.LoadXml(myXML)
Set xmlHttp = Nothing
Set objLst = xmlResponse.getElementsByTagName("item")
Set xmlResponse = Nothing
NoOfHeadlines = objLst.length - 1
Response.Write NoOfHeadlines
This worked find on my development server. When I moved it over to a staging server (which I have no control over, and no nothing about), NoOfHeadlines returns 0. It seems obvious to me that DomDocument is not working the way its supposed to.
Is this a version issue? How do I find out what version of DomDocument is on the staging server?
Is there another possibility?
The problem was
xmlHttp.Open "Get", myRSSfile, false
should be
xmlHttp.Open "GET", myRSSfile, false

Resources