Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
640 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

824 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