?
Solved

Convert VB6 MSXML2.ServerXMLHTTP process to C#

Posted on 2016-11-02
2
Medium Priority
?
146 Views
Last Modified: 2016-11-05
Need to convert VB6 MSXML2.ServerXMLHTTP process to C#.  I don't want to use MSXML2 so please don't provide solutions that add that DLL reference, I want the newest C# method that will replace the code below.  I have ran across links that discuss httpWebRequest & httpWebResponse but I don't know how to implement them to replace the code below.  Please help, thank you.


            
    Dim oXMLHTTP As MSXML2.ServerXMLHTTP
    Dim strURL As String
    Dim strURLFull As String
            
    strURL = "/this_server/ServerManager.srv"
    strURLFull = "http://192.168.1.1"  & strURL
    strData = "do_reboot=yes&servermanager_return_page=%2Fadmin%2Findex.shtml%3Fsel%3Dserver&servermanager_do=set_variables"
    
    oXMLHTTP.Open "POST", strURLFull, True
    oXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    oXMLHTTP.setRequestHeader "Connection", "Keep-Alive"
    oXMLHTTP.send (strData)
    
    If oXMLHTTP.readyState <> 4 Then
            oXMLHTTP.waitForResponse 3
    End If

Open in new window

0
Comment
Question by:Nathan Vanderwyst
[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
2 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 2000 total points
ID: 41871488
you can try use System.Net.WebRequest class to do the same thing.

WebRequest Class
https://msdn.microsoft.com/en-us/library/system.net.webrequest(v=vs.110).aspx

quick try:
using System.Net;
using System.IO;
using System.Text;
....
String response = String.Empty;
String URL = "http://192.168.1.1/this_server/ServerManager.srv";
String data = "do_reboot=yes&servermanager_return_page=%2Fadmin%2Findex.shtml%3Fsel%3Dserver&servermanager_do=set_variables";

                using (MemoryStream mem = new MemoryStream())
                {
                    WebRequest req = WebRequest.Create(URL);
                    req.Method = "POST";
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.Headers.Add("Connection", "Keep-Alive");
                    req.Timeout = 100000;

                    UTF8Encoding encoder = new UTF8Encoding();
                    byte[] postBytes = encoder.GetBytes(data);
                    req.ContentLength = postBytes.Length;

                   using (Stream requestDataStream = req.GetRequestStream())
                    {
                        requestDataStream.Write(postBytes, 0, postBytes.Length);
                        requestDataStream.Close();
                        using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
                        {
                            using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                            {
                                response = sr.ReadToEnd();
                                sr.Close();
                            }
                            resp.Close();
                        }
                        requestDataStream.Close();
                    }
                                     
                }

Open in new window

Similarly achievable using System.Net.HttpWebRequest class

HttpWebRequest Class
https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.110).aspx
0
 

Author Closing Comment

by:Nathan Vanderwyst
ID: 41875454
That worked like a charm.  Thank you so much.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 8 hours left to enroll

762 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