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
631 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
[X]
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
8 Comments
 
LVL 4

Expert Comment

by:sandy12879
ID: 33691701
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_
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 :)
0
 

Author Comment

by:Nebukad
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.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 52

Expert Comment

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

0
 

Author Comment

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

Expert Comment

by:rbgCODE
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..
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
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.
0
 

Accepted Solution

by:
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
</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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

735 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