Solved

Convert VB6 MSXML2.ServerXMLHTTP process to C#

Posted on 2016-11-02
2
78 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 52

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

739 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