Solved

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
8
627 Views
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 ----------------
<cfcomponent>
 <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("http://www.example.com/")#"></cfif>
  <cfobject action="Create" name="objSrvHTTP" class="MSXML2.ServerXMLHTTP">
  <cfset temp = objSrvHTTP.open("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>
</cffunction>
</cfcomponent>

Open in new window

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

Below an example of the script that call the web service. Note this should note be converted.
----
<cfinvoke webservice="http://www.example.com/web_services/webv2.cfc?wsdl" 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 = "http://www.example.com/" 
    set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
    xmlhttp.open "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.
0
Comment
Question by:Nebukad
8 Comments
 
LVL 4

Expert Comment

by:sandy12879
Comment Utility
http://forums.asp.net/p/962989/1226853.aspx
Check above link it will simply conver the CF to asp.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
(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 :)
0
 

Author Comment

by:Nebukad
Comment Utility
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.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
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?

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Nebukad
Comment Utility
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.
0
 
LVL 6

Expert Comment

by:rbgCODE
Comment Utility
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..
c_URL="http://site.com/cgi-dta/qun100.mbr/output?var="

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

Open in new window

0
 

Author Comment

by:Nebukad
Comment Utility
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.
0
 

Accepted Solution

by:
Nebukad earned 0 total points
Comment Utility
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
</script>
<%

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

Try
      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
      ms.st = src_status

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

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

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

End Try
%>

And the coldfusion cfc

<cfcomponent>
 <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="http://www.example.com/"></cfif>
  <cfhttp URL="[URL_OF_ASPX_PAGE]">
     <cfhttpparam type="Formfield" value="#pass_url#" name="weburl">
  </cfhttp>
  <cfset getUrlData = XmlParse(cfhttp.fileContent)>
  <cfset response_url = #getUrlData.page.url.XmlText#>
  <cfset response_status = #getUrlData.page.status.XmlText#>
  <cfset response_body = #getUrlData.page.body.XmlText#>
  <cfset response_characterset = #getUrlData.page.characterset.XmlText#>
  <cfset response_error = #getUrlData.page.error.XmlText#>
  <cfset websitedata=structnew()>
  <cfset websitedata.responseBody = #resetXML(response_body)#>
  <cfset websitedata.status = #left(response_status,3)#>
  <cfreturn websitedata>
 </cffunction>
 <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>
 </cffunction>
</cfcomponent>

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now