How do I write a simple ASP webservice (or page) that returns the "status" and "content" of an url in a structure/map object

Posted on 2010-09-16
Last Modified: 2012-05-10
I need to convert to following coldfusion code into its asp equivalent. The code fetches data from an url and returns the page and its status back to the calling page in a structure.

---------- THE CODE THAT NEEDS TO BE CONVERTED TO ASP ----------------
 <cffunction name="getSite" access="remote" returntype="struct" output="no">
  <cfargument name="pass_url" type="string" required="yes">
  <cfif not isdefined("pass_url")><cfset pass_url="#urlencodedformat("")#"></cfif>
  <cfobject action="Create" name="objSrvHTTP" class="MSXML2.ServerXMLHTTP">
  <cfset temp ="GET","#URLDecode(pass_url)#")>
  <cfset temp = objSrvHTTP.send("")>
<cfset websitedata=structnew()>

  <cfset websitedata.responseBody = #toString(objSrvHTTP.responseBody)#>
  <cfset websitedata.status = #left(objSrvHTTP.Status,3)#>
  <cfreturn websitedata>

Open in new window

---------- END ----------------

Below an example of the script that call the web service. Note this should note be converted.
<cfinvoke webservice="" method="getSite" returnvariable="objSrvHTTP">
<cfinvokeargument name="pass_url" value="#bron_url#"/></cfinvoke>
<cfreturn objSrvHTTP.responseBody>

Open in new window


This is what i got so far. The code should return the responseText and status in a structure like the coldfusion script. As you can guess i know very little of asp.
    url = "" 
    set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") "GET", url, false 
    xmlhttp.send "" 
    dim status
    status =  xmlhttp.status 
    dim responseBody
    responseBody = xmlhttp.responseText 
    set xmlhttp = nothing 

Open in new window

Well hope anyone can help.
Question by:Nebukad
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 33691701
Check above link it will simply conver the CF to asp.
LVL 52

Expert Comment

ID: 33691959
(no points.. just a small comment)

Jumping to a totally different engine just to convert 1 function may be a bit extreme.

BTW: Sorry I'm not an ASP expert. So I can't help with that part.  But since you're not using the expected cfhttp here, your job's a little easier. The existing code (MSXML2.ServerXMLHTTP) is almost ASP already :)

Author Comment

ID: 33693045
sandy12879: As _agx_ already mentioned jumping to a different engine is not  what i am looking for. I simply what to convert this script to asp nothing more nothing less.
_agx_: FYI  in our experience the MSXML2.ServerXMLHTTP code is much more stable then the cfhttp equivalent. However unfortunately the coldfusion 64 bit server does not support 32 bit components. Now i am running this script on a different server as a webservice to overcome this issue.
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

LVL 52

Expert Comment

ID: 33694700
Yeah, it has some limitations.  For most stuff it's okay. I use lower level java lib's for the rest.  Wish I could help with the .asp part.  Maybe rephrase the question and ask - how do I write a simple ASP webservice (or page) that returns the "status" and "content" in a structure/map object?


Author Comment

ID: 33695390
Unfortunately it is difficult to switch methods because different methods tend to return differents source documents e.g. in some cases cfhttp returns a different source code then MSXML2.ServerXMLHTTP.does. Because a lot of data is already stored based on the source code returned by MSXML2.ServerXMLHTTP i am not eager to switch. Anyway i followed your advice and have requested to change the title. This will hopefully get the attentions of an asp programmer.

Expert Comment

ID: 33696721
If I had to convert that code into asp, and that is all, nothing more nothing less, this is what it would look like..

yes, that simple and basic..

Dim objXMLHTTP, xml
 Set xml = Server.CreateObject("Microsoft.XMLHTTP")
 'Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP") 'newer version 3
  xml.Open "POST", c_url, False
 Set xml = Nothing

Open in new window


Author Comment

ID: 33698983
rbgCODE: Thanks for your response, but this solution does not contain all the data of the website just the html/xml part. I also want the response status and in the future perhaps other technical details of the web page. Futhermore returning the data in a structure keeps the data returned intact. If i would just output the data as you suggested i would still need to use cfhttp to get the response. And the reason that i am using Microsoft.XMLHTTP is to prevent having to use cfhttp (which is imo an badly written tag in coldfusion).

So basically i just want to write a simple asp web service that uses Microsoft.XMLHTTP to fetch data and return this data in a structure/map. This way i can call this webservice in coldfusion without having to use cfhttp  and handle the data there.

Accepted Solution

Nebukad earned 0 total points
ID: 33753684
Solved it by creating a coldfusion webservice that connects to an apsx page thats connects to a webpage and returns the data in xml. I know very little of asp(x) so the code i have written can probably be improved.

Here the aspx file

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web.UI.Page" %>
<%@ Page Language="VB" %>

<script runat="server" language="vb">
Structure serviceResponse
Public st As String
Public bo As String
Public en As String
Public ur As String
End Structure

Response.ContentType = "text/xml"
Dim ms As New serviceResponse

      Dim weburl As String = Request.QueryString("weburl")
      ms.ur = weburl
      Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(weburl), HttpWebRequest)

      myHttpWebRequest.AllowAutoRedirect = False
      myHttpWebRequest.KeepAlive = False
      myHttpWebRequest.Headers.Set("Pragma", "no-cache")

      Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
      Dim src_enc As String = myHttpWebResponse.CharacterSet
      Dim src_status As String  = myHttpWebResponse.StatusCode

      ms.en = src_enc = src_status

    Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream()
      Dim readStream As New StreamReader(receiveStream, Encoding.GetEncoding(myHttpWebResponse.CharacterSet)) = readStream.ReadToEnd() = Replace(,"<","~@^lt;^@~") = Replace(,">","~@^gt;^@~") = Replace(,"&","~@^amp^@~")

      Response.Write("<?xml version=""1.0""?><page><error>NO</error><errormessage></errormessage><url>" + ms.ur + "</url><body>" + + "</body><status>" + +  "</status><characterset>" + ms.en + "</characterset></page>")

Catch e As WebException
      Dim webResponse As HttpWebResponse = e.Response        = CType(webResponse.StatusCode,Integer) = ""
      ms.en = ""
      Response.Write("<?xml version=""1.0""?><page><error>NO</error><errormessage></errormessage><url>" + ms.ur + "</url><body></body><status>" + +  "</status><characterset>" + ms.en + "</characterset></page>")

Catch e As Exception

Response.Write("<?xml version=""1.0""?><page><error>YES</error><errormessage>" + e.message + "</errormessage><url>" + ms.ur + "</url><body>" + + "</body><status>" + +  "</status><characterset>" + ms.en + "</characterset></page>")

End Try

And the coldfusion cfc

 <cffunction name="getSitev2" access="remote" returntype="struct" output="no">
  <cfargument name="pass_url" type="string" required="yes">
  <cfif not isdefined("pass_url")><cfset pass_url=""></cfif>
  <cfhttp URL="[URL_OF_ASPX_PAGE]">
     <cfhttpparam type="Formfield" value="#pass_url#" name="weburl">
  <cfset getUrlData = XmlParse(cfhttp.fileContent)>
  <cfset response_url =>
  <cfset response_status =>
  <cfset response_body =>
  <cfset response_characterset =>
  <cfset response_error =>
  <cfset websitedata=structnew()>
  <cfset websitedata.responseBody = #resetXML(response_body)#>
  <cfset websitedata.status = #left(response_status,3)#>
  <cfreturn websitedata>
 <cffunction name="resetXML" returntype="string">
  <cfargument name="body" required="yes">
  <cfset body = replace(body,'~@^lt;^@~','<','all')>
  <cfset body = replace(body,'~@^gt;^@~','>','all')>
  <cfset body = replace(body,'~@^amp^@~','&','all')>
  <cfreturn body>


Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Html Table looping 4 42
Date on a table 16 45
VB script help 23 50
Error when uploading a document from an IPad 6 26
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question