Solved

Convert VB6 MSXML2.ServerXMLHTTP process to C#

Posted on 2016-11-02
2
57 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 50

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

792 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