Need to convert VB6 code in .net (c# or VB.net)

Hi there,

I get this peace of code in VB6 and I would like to translate it in C#.
It's about sending an xml request to a web service server (Apache TOMCAT) with http post method (non-SOAP).
 
I try to rewrite it in c# but it's not work. No error but not doing what it's suppose to do.

Best regards.
////////////////////////////////////////
/  VB6 Part
///////////////////////////////////////

' MSXML2.ServerXMLHTTP compoment : send/receive http request to/from Web service server
' @remarks CreateObject("MSXML2.ServerXMLHTTP")
Public mXmlHttp As Object
'
'
' URL to the web service
Public URLToServ As String
'
'
' Submit request template
Public mSubmitRequest As String


'------------------------------------------------------------------------------
'
' Functions and subroutines
'
'------------------------------------------------------------------------------
'
' Initialize ServerXMLTTP
'
' @return True if ServerXMLHTTP is created otherwise False.

Public Function MDLCONN_Initialize() as Boolean

On Error Resume Next
Set mXmlHttp = CreateObject("MSXML2.ServerXMLHTTP")
mXmlHttp.setTimeouts 0, 0, 0, 0
Set MDLCONN_Initialize = (Not mXmlHttp Is Nothing)

End Function

'
' Send a XML request to the server
'
' @param xmlStr XML request.
' @return server response in an MSXML2.DOMDocument object or NULL.

Public Function MDLCONN_SendXmlRequest(ByVal MyXMLRequest As String) As Object

Dim xmlDoc As Object
Dim URL As String
Dim body() As Byte
On Error GoTo ErrHandler

' Verifying the xml request
Set xmlDoc = CreateObject("MSXML2.DOMDocument") 

If Not xmlDoc.loadXML(MyXMLRequest) Then
Err.Raise vbObjectError + 10, "mdlConnector.MDLCONN_SendXmlRequest", "Unable to load request string in DomDocument: " & xmlDoc.parseError.reason & vbCrLf & MyXMLRequest
GoTo CleanHandler
End If

'
' convert special characters
' 
MyXMLRequest = Replace(MyXMLRequest, "%", "%25") 
MyXMLRequest = Replace(MyXMLRequest, "&", "%26") 
MyXMLRequest = Replace(MyXMLRequest, "+", "%2B")

'
' transform xml request in Bytes array
' 
body = StrConv("priority=1&format=XML&request="&MyXMLRequest, vbFromUnicode)
URLToServ = "http://localhost/webservices/" 
URL = URLToServ & "submit"
mXmlHttp.Open "POST", URL, True
mXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1"
mXmlHttp.send CVar(body)

Do While mXmlHttp.readyState <> 4
mXmlHttp.waitForResponse 1000
DoEvents
Loop

If mXmlHttp.status <> 200 Then
Err.Raise vbObjectError + 11, "mdlConnector.MDLCONN_SendXmlRequest", "Failed to reach connector services: " & mXmlHttp.status
Set MDLCONN_SendXmlRequest = Nothing
GoTo CleanHandler
Else
mXmlHttp.responseXML.Load mXmlHttp.responseBody
Set MDLCONN_SendXmlRequest = mXmlHttp.responseXML
End If

GoTo CleanHandler

ErrHandler:
' default error
Err.Raise vbObjectError + 20, "mdlConnector.MDLCONN_SendXmlRequest", "Error in procedure MDLCONN_SendXmlRequest of Module mdlConnector"

CleanHandler:
' clean objects
Set xmlDoc = Nothing

End Function



/////////////////////////////////////
/  C# test
////////////////////////////////////

            string xml = "&lt;?xml version='1.0' encoding='UTF-8'?><submitRequests service='businessService' sessionLogin='genericname@abc.com' sessionPassword='pwd123'><requests><removeCustomers deleteOrders='false'><customerIds><value>C1111</value></customerIds></removeCustomers></requests></submitRequests>";
            string url = "http://zzz.zzz.zzz.zz:zz/generic/nnnnn";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            string s = "priority=1&format=XML&request=" + HttpUtility.UrlEncode(xml);
            byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(s);
            req.Method = "POST";
            req.ContentType = "text/xml;charset=utf-8";
            req.ContentLength = requestBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
            string backstr = sr.ReadToEnd();
            sr.Close();
            res.Close();

Open in new window

LeizahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

newhottaCommented:
Hi,

You need to use escape characters for the single quote ' (i.e. replace ' in your string to be \')

for more information in escape characters:

http://msdn.microsoft.com/en-us/library/h21280bw.aspx


0
LeizahAuthor Commented:
I resend the 2 code separatly for better comprehension.

The VB6 code to convert in .net :
Public mXmlHttp As Object 

Public URLToServ As String 
 
Public mSubmitRequest As String 
 
 
Public Function MDLCONN_Initialize() as Boolean 
 
On Error Resume Next 
Set mXmlHttp = CreateObject("MSXML2.ServerXMLHTTP") 
mXmlHttp.setTimeouts 0, 0, 0, 0 
Set MDLCONN_Initialize = (Not mXmlHttp Is Nothing) 
 
End Function 
 
 
Public Function MDLCONN_SendXmlRequest(ByVal MyXMLRequest As String) As Object 
 
Dim xmlDoc As Object 
Dim URL As String 
Dim body() As Byte 
On Error GoTo ErrHandler 
 
Set xmlDoc = CreateObject("MSXML2.DOMDocument")  
 
If Not xmlDoc.loadXML(MyXMLRequest) Then 
Err.Raise vbObjectError + 10, "mdlConnector.MDLCONN_SendXmlRequest", "Unable to load request string in DomDocument: " & xmlDoc.parseError.reason & vbCrLf & MyXMLRequest 
GoTo CleanHandler 
End If 
  
MyXMLRequest = Replace(MyXMLRequest, "%", "%25")  
MyXMLRequest = Replace(MyXMLRequest, "&", "%26")  
MyXMLRequest = Replace(MyXMLRequest, "+", "%2B") 
 
body = StrConv("priority=1&format=XML&request="&MyXMLRequest, vbFromUnicode) 
URLToServ = "http://localhost/webservices/"  
URL = URLToServ & "submit" 
mXmlHttp.Open "POST", URL, True 
mXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1" 
mXmlHttp.send CVar(body) 
 
Do While mXmlHttp.readyState <> 4 
mXmlHttp.waitForResponse 1000 
DoEvents 
Loop 
 
If mXmlHttp.status <> 200 Then 
Err.Raise vbObjectError + 11, "mdlConnector.MDLCONN_SendXmlRequest", "Failed to reach connector services: " & mXmlHttp.status 
Set MDLCONN_SendXmlRequest = Nothing 
GoTo CleanHandler 
Else 
mXmlHttp.responseXML.Load mXmlHttp.responseBody 
Set MDLCONN_SendXmlRequest = mXmlHttp.responseXML 
End If 
 
GoTo CleanHandler 
 
ErrHandler: 
 
Err.Raise vbObjectError + 20, "mdlConnector.MDLCONN_SendXmlRequest", "Error in procedure MDLCONN_SendXmlRequest of Module mdlConnector" 
 
CleanHandler: 
 
Set xmlDoc = Nothing 
 
End Function

Open in new window

0
LeizahAuthor Commented:
I've found the solution.
The c# part was good. The problem was on the content type.

I've modified this part :

byte[] requestBytes = System.Text.Encoding.ASCII.GetByte("priority=1&format=XML&request="+xml);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";

This peace of code work for me.
string xml = "&lt;?xml version='1.0' encoding='UTF-8'?><submitRequests service='businessService' sessionLogin='genericname@abc.com' sessionPassword='pwd123'><requests><removeCustomers deleteOrders='false'><customerIds><value>C1111</value></customerIds></removeCustomers></requests></submitRequests>";
string url = "http://zzz.zzz.zzz.zz:zz/generic/nnnnn";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

byte[] requestBytes = System.Text.Encoding.ASCII.GetByte("priority=1&format=XML&request="+xml);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr.ReadToEnd();
sr.Close();
res.Close();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.