Link to home
Start Free TrialLog in
Avatar of ube100
ube100Flag for United Kingdom of Great Britain and Northern Ireland

asked on

vb script to C#

Hi, Can somebody help me converting this VB script into C# please:

Dim sResponse
Dim oHTTP

 Set oHTTP = CreateObject("MSXML2.XMLHTTP")

oHttp.Open "POST", "http://secure.voice-pay.com/gateway/remote_auth", False
oHttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oHttp.Send sRequestString

sResponse  = oHttp.responseText
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this:
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://secure.voice-pay.com/gateway.remote_auth");
            req.ContentType = "application/x-www-form-urlencoded";

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            string result;
            using (System.IO.StreamReader reader = new System.IO.StreamReader(resp.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }

Open in new window

Alternative you can use HttpRequest class in c#:

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://secure.voice-pay.com/gateway/remote_auth");

HttpWebResponse rs = (HttpWebResponse)wr.GetResponse();
Stream s = rs.GetResponseStream();
StreamReader srt = new StreamReader(s);
receiveData = srt.ReadToEnd();

Avatar of ube100

ASKER

Guys,

How do I set the request string?

oHttp.Send sRequestString

As you can see I'm setting the request string on the above line but in your C# code there is nothing there.
You need to grab the RequestStream and write your data to it:
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://secure.voice-pay.com/gateway.remote_auth");
            req.ContentType = "application/x-www-form-urlencoded";

            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(req.GetRequestStream()))
            {
                writer.Write(sRequestString);
            }

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            string result;
            using (System.IO.StreamReader reader = new System.IO.StreamReader(resp.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }

Open in new window

Avatar of ube100

ASKER

On this line:

(System.IO.StreamWriter writer = new System.IO.StreamWriter(req.GetRequestStream()))

getting this error:

cannot send a content-body with this verb- type.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ube100

ASKER

Thanx!!!