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/")%>
paygoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

drichardsCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
drichardsCommented:
Also, you can put the function in the code behind that goes with the page.
0
GwynforWebCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

drichardsCommented:
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
drichardsCommented:
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
paygoAuthor Commented:
thanks drichards,

this is correct - but I get an msxml4.dll access is denied on some sites.
0
drichardsCommented:
Is that an HTTP status 401 or 403?  You can check for those as well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.