Solved

Convert VB6 MSXML2.ServerXMLHTTP process to C#

Posted on 2016-11-02
2
33 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
2 Comments
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now