Link to home
Start Free TrialLog in
Avatar of paygo
paygo

asked on

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

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/")%>
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drichards
drichards

Also, you can put the function in the code behind that goes with the page.
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>
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/"))%>
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.
Avatar of paygo

ASKER

thanks drichards,

this is correct - but I get an msxml4.dll access is denied on some sites.
Is that an HTTP status 401 or 403?  You can check for those as well.