Solved

Convert VB6 MSXML2.ServerXMLHTTP process to C#

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to read json value 2 48
Set value of dynamically created checkboxlist 2 31
.NET 2008 VB and C# 6 27
tableview is not updating 1 6
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
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…

816 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

13 Experts available now in Live!

Get 1:1 Help Now