Using VBScript with ASP I am trying to set up an HTTP GET Request which will visit a page which in turn generates a line of ASCII (non-HTML). I then want to extrapolate that ASCII line which will have 4 values delimited by semicolons back into 4 variables in my original ASP page so that I can take those values and do something with them.
This is the page I want to access with HTTP GET Request http://www.certigo.com/demo/request.asp. Three of the values are null here.
I don't know much/anything about ASP, so I have this:
Dim oXMLHTTP
Dim strStatusTest
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
oXMLHTTP.Open "GET", "http://www.certigo.com/demo/request.asp", False
oXMLHTTP.Send
If oXMLHTTP.Status = 200 Then
strStatusText = oXMLHTTP.responseBody
End If
but obviously I haven't a clue what I'm doing because this isn't working at all. I would be totally unsurprised to learn that what I have here isn't going in the right direction. Please help!!
-Tracy
Your code should look like this:-
Function GetTextFromUrl(url)
Dim oXMLHTTP
Dim strStatusTest
Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXMLHTTP.Open "GET", url, False
oXMLHTTP.Send
If oXMLHTTP.Status = 200 Then
GetTextFromUrl = oXMLHTTP.responseText
End If
End Function
Dim sResult : sResult = GetTextFromUrl("http://www.certigo.com/demo/request.asp")
Note use ServerXMLHTTP from within ASP, the XMLHTTP component is designed for client side usage and isn't safe to use in the multithreaded environment such as ASP.
Try changing the oXMLHTTP.responseBody to oXMLHTTP.responseText and see if that works.
Refer to this web page if you need some more information on this technique:
http://classicasp.aspfaq.com/general/how-do-i-read-the-contents-of-a-remote-web-page.html.
Related
I have a bog standard WebAPI that accepts a POST and takes those parameters and processes certain things. I'm not certain if the problem is the VBA or the ASP.Net WebAPI so I am cross posting.
I've used Postman to test the API and it works fine when I post Key/Value params.
I've also tried the following method and relevant parameters and get the same result:
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
I'm having problems getting it to work with the VBA, when debugging in Visual Studio on the WebAPI I cannot see the values I am posting anywhere. It's like the request is coming in blank. My WebAPI is subsequently throwing an error because the parameters are missing and certain parameters are required.
I'm not sure if this is a problem on the VBA side or the ASP.NET WebAPI side so I am cross posting in a hope someone can highlight or spot what I am doing wrong.
Private Sub Command4_Click()
Dim argumentString1 As String
argumentString1 = "companyId=228&secondsToLog=15&subject=TestBackup123&description=TestDescription" & _
"&category=&tag=&ticketType=task&assignee=gavin&requesterEmail=bob#smith.com" & _
"&submitterName=gavin&status=open&priority=normal"
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://localhost:64874/api/zendeskhelper"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.send (argumentString1)
txtresult = objHTTP.responsetext & ": " & argumentString1
End Sub
The Web API structure looks like this:
public HttpResponseMessage Post([FromUri] TicketBody ticket)
{
// Nothing is bound to ticket like it is in Postman
}
Any help or pointers would be much appreciated!
The way you are sending your data, you need to remove [FromUri]. When using [FromUri], the ASP.NET engine will look for data in the Uri, not the body of the request.
public HttpResponseMessage Post(TicketBody ticket)
{
// 'ticket should not be null now
}
On the other hand, if you need to keep [FromUri] you could change your call to:
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://localhost:64874/api/zendeskhelper?" & argumentString1
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.send ("")
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/"" />")
In my code I'm sending a HttpWebRequest to a page in my website.
When request sends to this page, It doesn't maintain the Session values.
Below is the code, from where I'm generating the web request:
Public Overloads Shared Function ReadURL(ByVal sUrl As String) As String
Dim sBody As String
Dim oResponse As HttpWebResponse
Dim oRequest As HttpWebRequest
Dim oCookies As New CookieContainer
oRequest = WebRequest.Create("http://localhost:64802/inventory/purchase_order.aspx?id=5654")
oRequest.CookieContainer = oCookies
oResponse = oRequest.GetResponse()
Dim oReader As New StreamReader(oResponse.GetResponseStream())
sBody = oReader.ReadToEnd
oReader.Close()
oResponse.Close()
Return sBody
End Function
Below is the code written on Page_Load of Purchaseorder.aspx.vb:
iDomains_ID = Session("Domains_ID")
iLogin_ID = Session("Login_ID")
sPage = Request.Path
If Request.QueryString.Count > 0 Then sPage &= "?" & Request.QueryString.ToString()
sPage = shared01.Encrypt(sPage, Application("PK"))
If Not User.Identity.IsAuthenticated Or iLogin_ID = 0 Then
Response.Redirect("/login.aspx?page=" & sPage)
Exit Sub
End If
Above code doesn't gets the session values and it redirects to the login page.
So, how i can maintain the session on both pages during HttpWebRequest.
Looking for your replies.
EDIT
I've tried to use CookieContainer class as you can see in above code. But it doesn't work at all.
As an alternative, assuming the calling and called pages are in the same application, you could use the Server.Execute method to load the content of the page without making a separate request to the site:
Public Overloads Function ReadURL(ByVal sUrl As String) As String
Using writer As New StringWriter()
Server.Execute("~/inventory/purchase_order.aspx?id=5654", writer, False)
Return writer.ToString()
End Using
End Function
If I've understood you correctly, you're making a request from one page in your site to another, and you want to send the cookies from the current HttpRequest with your WebRequest?
In that case, you'll need to manually copy the cookies to the CookieContainer:
For Each key As String In Request.Cookies.AllKeys
Dim sourceCookie As HttpCookie = Request.Cookies(key)
Dim destCookie As New Cookie(sourceCookie.Name, sourceCookie.Value, sourceCookie.Path, "localhost")
destCookie.Expires = sourceCookie.Expires
destCookie.HttpOnly = sourceCookie.HttpOnly
destCookie.Secure = sourceCookie.Secure
oCookies.Add(destCookie)
Next
NB: You'll either need to make the ReadUrl function non-Shared, or pass the current HttpRequest as a parameter.
You'll also need to make sure the calling page has EnableSessionState="false" in the <%# Page ... %> directive, otherwise the page you're calling will hang trying to obtain the session lock.
Your code seems like you will need to make a request and a post. The first request will redirect you to your login page. The second will be a request where you post to the login page, which will start the session and (?) store information into the session variables. That post (to the login page) will then redirect you to the page you want.
I used code in this example http://www.codeproject.com/Articles/145122/Fetching-ASP-NET-authenticated-page-with-HTTPWebRe (I tweaked it a bit) to write an application to do this.
How can I asynchronously read XMLHTTP before it’s finished in VBS? The example
below (which has a ”The data necessary to complete this operation is not yet available”
error if sleep times are not long enough) uses a big Wikipedia page, but ultimately I
am thinking to use this for an infinite webpage/stream that never finishes loading. So,
is there a simple way to modify this example to get partial HTTP responses early?
strFileURL = "https://en.wikipedia.org/wiki/Algorithm" ' or any big webpage
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") ' all of these xmlhttp options have no "streaming success"
' Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")
' Set objXMLHTTP = CreateObject("Microsoft.XmlHttp")
' Set objXMLHTTP = Createobject("MSXML2.XMLHTTP.3.0")
' Set objXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")
objXMLHTTP.open "GET", strFileURL, true
objXMLHTTP.send()
' Is there something other than ResponseText to avoid error and get partial result below (i.e., read html like a stream)?
WScript.Sleep 500 ' need to play with this time to try to cause mystate1=3 below
mystate1 = objXMLHTTP.ReadyState ' should be 3
myscan = objXMLHTTP.ResponseText ' CAUSES ERROR if mystate1 < 4 (comment this line out to avoid error)
WScript.Sleep 1000
mystate2 = objXMLHTTP.ReadyState ' should be 4
myscan = objXMLHTTP.ResponseText
MsgBox(mystate1)
MsgBox(mystate2)
MsgBox(myscan)
Set objXMLHTTP = Nothing
That's not possible, at least not by using an XMLHTTPRequest. As documented, neither responseText nor responseBody are available before the request is completed.
So I am writing a web application for use within my organization. The application requires that it know who the current user is. This is done by calling the Request.ServerVariables("AUTH_USER") function, which works great as long as 'Anonymous Access' is disabled (unchecked) and 'Integrated Windows Authentication' is enabled (checked) within IIS for this subweb.
Unfortunately by doing this I get an 'Access Denied' error when I hit the load method of the XML DOM.
Example code:
dim urlToXmlFile
urlToXmlFile = "http://currentwebserver/currentsubweb/nameofxml.xml"
dim xmlDom
set xmlDom = Server.CreateObject("MSXML2.DOMDocument")
xmlDom.async = false
xmlDom.load( urlToXmlFile ) ' <-- this is where I get the error!
I've looked everywhere and cannot find a solution. I should be able to load an XML file into the DOM regardless of the authentication method.
Any help would be appreciated. So far the only two solutions I can come up with are:
a) create a new subweb that JUST gets the current user name and somehow passes it back to my XML reading subweb.
b) open up security on the entire system to 'Everyone', which works but our IS department wouldn't care for that.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Here was my original code, which cause the access denied error:
dim urlToXml
urlToXml = "http://someserver/somesomeweb/nameofxml.xml"
dim xmlDom
set xmlDom = Server.CreateObject("MSXML2.DOMDocument")
xmlDom.loadXML( urlToXml )
dim xsl
set xsl = Server.CreateObject("MSXML2.DOMDocument")
xsl.async = false
xsl.load(server.MapPath("somexsl.xsl"))
Response.Write( xmlDom.transformNode(xsl) )
xmlDom.save( server.MapPath("accounting/somexml.xml") )
Now, here is my new code thanks to thomask:
dim urlToXml
urlToXml = "http://someserver/somesomeweb/nameofxml.xml"
set http = CreateObject("MSXML2.ServerXMLHTTP.3.0")
http.Open "GET", urlToXml, false
http.Send()
dim xmlDom
set xmlDom = Server.CreateObject("MSXML2.DOMDocument")
xmlDom.loadXML( http.responseXML.xml )
dim xsl
set xsl = Server.CreateObject("MSXML2.DOMDocument")
xsl.async = false
xsl.load(server.MapPath("somexsl.xsl"))
Response.Write( xmlDom.transformNode(xsl) )
xmlDom.save( server.MapPath("newxml.xml") )
Again thank you very much thomask.
You might wanna look at MSXML2.ServerXMLHTTP(.3.0 - 6.0) to specify the user credentials. If the Content-Type is configured correctly, ServerXMLHTTP should give you the DOMDocument in the responseXml property.
Dim http
Set http = CreateObject("MSXML2.ServerXMLHTTP.3.0")
http.Open("GET", "http://currentwebserver/currentsubweb/nameofxml.xml", false, "user", "pass")
http.Send()