?
Solved

MSXML2.ServerXMLHTTP with C# Scripting? HOW TO???

Posted on 2004-11-28
7
Medium Priority
?
16,933 Views
Last Modified: 2012-05-05
Hi, I know C#, but not VB.  
How do I create the equivalent C# script for this VB Syntax?

Set xml Server.CreateObject("MSXML2.ServerXMLHTTP")

Thanks

complete function is here. but I can do the rest out, once I get the correct Objects.


<%
Function GetContent(url)
    Dim xmlHttp
    Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
    xml.Open "GET", url, True
    Call xml.Send()

      If xml.readyState <> 4 Then
        xml.waitForResponse 3
    End If
   
     If Err.Number <> 0 Then
    Else
         If (xml.readyState <> 4) Or (xml.Status <> 200) Then
              xml.Abort
              strData = "Problem communicating with remote server..."
         Else
              strData = xml.ResponseText
         End If
    End If
    GetContent = strData
End Function
%>

<%=GetContent("http://oldlook.experts-exchange.com/")%>
0
Comment
Question by:paygo
  • 5
7 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 2000 total points
ID: 12693285
I think this is approximately the C# equivalent of your VBScript.

<%
        string GetContent( string url)
        {
            string result = "Error communicating with server";
            System.Net.HttpWebRequest wreq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            wreq.Method = "GET";
            wreq.Timeout = 3000;
            System.Net.HttpWebResponse wr = (System.Net.HttpWebResponse)wreq.GetResponse();

            if (wr.StatusCode == System.Net.HttpStatusCode.OK)
            {
                System.IO.Stream s = wr.GetResponseStream();
                System.Text.Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                System.IO.StreamReader readStream = new System.IO.StreamReader( s, enc );
                result = readStream.ReadToEnd();
            }
            return result;
        }
%>

<% Response.Output.Write(GetContent("http://oldlook.experts-exchange.com/"))%>
0
 
LVL 19

Expert Comment

by:drichards
ID: 12693291
Also, you can put the function in the code behind that goes with the page.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12696359
try this in JS

<body onload="init()">
<input value="view Yahoo" type="button" onclick="alert(document.getElementById('yahoo').innerHTML)">
<input value="view ebay"type="button" onclick="alert(document.getElementById('ebay').innerHTML)"> <p>
<script>
function LoadPage(theURL,theID){
   var objSrvHTTP;
    try{
          objSrvHTTP = new ActiveXObject("Msxml2.XMLHTTP.4.0");
         
     }
     catch(e){    
          try{
               objSrvHTTP = new ActiveXObject("Msxml2.XMLHTTP");
          }
               
          catch(e){
               try{              
                    objSrvHTTP = new ActiveXObject("Microsoft.XMLHTTP");
                    alert("ok you have Microsoft.XMLHTTP")
               }                  
               catch(e){              
                    alert("you need install MSXML 4.0, http://www.microsoft.com/downloads/");
                    return;
               }    
          }
     }
       objSrvHTTP.open ("GET",theURL,false);
       objSrvHTTP.send ("");
       document.getElementById(theID).innerHTML=objSrvHTTP.responseText;
}

function init(){
     LoadPage("http://www.yahoo.com","yahoo")
     LoadPage("http://www.ebay.com","ebay")
}
</script>
0
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!

 
LVL 19

Expert Comment

by:drichards
ID: 12697237
One note on the C# script, you need to put the script tags around it, not the <% %>.  Also, it should go inside the html <head></head>. :

    <script runat="server >
    ... function here...
    </script>

Then in the body:

<% Response.Output.Write(GetContent("http://oldlook.experts-exchange.com/"))%>
0
 
LVL 19

Expert Comment

by:drichards
ID: 12697366
One of these times I'll get it right - missed a closing quote:

    <script runat="server" >
    ... function here...
    </script>

Or put it in the code behind as previously indicated.
0
 

Author Comment

by:paygo
ID: 12697478
thanks drichards,

this is correct - but I get an msxml4.dll access is denied on some sites.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12697822
Is that an HTTP status 401 or 403?  You can check for those as well.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

578 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