none
VB Script to C# conversion RRS feed

  • Question

  • Hello all,

    I have below code in VB Script. We are moving it out to C#, how can I achieve this in C#

    xmlhttp.open "POST", URL, false  
    			xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=UTF-8"   
    			xmlhttp.setRequestHeader "SOAPAction", "urn:retrieveCoverDecision"
    			xmlhttp.send XMlMessage
    If xmlhttp.Status = 12002 Then 
    				'Time Out
    XMLResponse = xmlhttp.responseText
    Set xmlhttp = nothing

    Any help very much appreciated. Thank you

    Thanks & Regards,
    Manjunath C Bhat,
    http://manjunathcbhat.blogspot.com
    http://manjunathcbhat.wordpress.com

    Monday, December 16, 2013 3:15 PM

Answers

  • I think it goes mostly like this:

    string xml = "<test/>";
    HttpWebRequest request = HttpWebRequest.CreateHttp( URL );
    request.Method = "POST";
    request.Headers.Add( "SOAPAction", "urn:retrieveCoverDecision" );
    
    byte[] requestBytes = Encoding.UTF8.GetBytes( xml );
    request.ContentType = "text/xml";
    request.ContentLength = requestBytes.Length;
    Stream outgoingDataStream = request.GetRequestStream();
    outgoingDataStream.Write( requestBytes, 0, requestBytes.Length );
    outgoingDataStream.Close();
                
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Debug.WriteLine( response.StatusCode ); // check status code here
    var stream = response.GetResponseStream();
    StreamReader sr = new StreamReader( stream );
    string responseText = sr.ReadToEnd();

    Not sure about that exact status code, but the status code is in response.StatusCode.

    Monday, December 16, 2013 3:50 PM
  • Hi All,

    I got my issue resolved. It was the proxy which was troubling me. I added below code and now I am able to download the data.

    myXmlhttp.Proxy = new WebProxy() { UseDefaultCredentials = true };
                myXmlhttp.Proxy = WebRequest.DefaultWebProxy;
                myXmlhttp.Credentials = new NetworkCredential("myUserName", "myPassword", "myDomain");
                myXmlhttp.Proxy.Credentials = new NetworkCredential("myUserName", "myPassword", "myDomain");

    Thank you all for your kind responses as always.


    Thanks & Regards,
    Manjunath C Bhat,
    http://manjunathcbhat.blogspot.com
    http://manjunathcbhat.wordpress.com

    Thursday, December 19, 2013 1:18 PM

All replies

  • It appears that you're making a SOAP call.  In general you'll simply add a web reference to your project which will generate the necessary code to make the SOAP call like it was a normal .NET object.  If you don't want to go that route for whatever reason then you'll have to build it by hand using WebRequest.  There are many examples of how to make a SOAP call using this type but the details vary depending upon what you're calling, whether there are parameters and in handling the results.  Here's a few starter links:

    http://mikehadlow.blogspot.com/2006/05/making-raw-web-service-calls-with.html
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/ed36e83f-9b0a-4a73-8ca3-1b9ecb524f06/how-to-construct-a-soap-service-request-using-c-for-httpwebrequest?forum=wcf

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Monday, December 16, 2013 3:28 PM
    Moderator
  • I think it goes mostly like this:

    string xml = "<test/>";
    HttpWebRequest request = HttpWebRequest.CreateHttp( URL );
    request.Method = "POST";
    request.Headers.Add( "SOAPAction", "urn:retrieveCoverDecision" );
    
    byte[] requestBytes = Encoding.UTF8.GetBytes( xml );
    request.ContentType = "text/xml";
    request.ContentLength = requestBytes.Length;
    Stream outgoingDataStream = request.GetRequestStream();
    outgoingDataStream.Write( requestBytes, 0, requestBytes.Length );
    outgoingDataStream.Close();
                
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Debug.WriteLine( response.StatusCode ); // check status code here
    var stream = response.GetResponseStream();
    StreamReader sr = new StreamReader( stream );
    string responseText = sr.ReadToEnd();

    Not sure about that exact status code, but the status code is in response.StatusCode.

    Monday, December 16, 2013 3:50 PM
  • Hi,

    Thank you, I think it worked but at line

    Stream outgoingDataStream = request.GetRequestStream();

    Its throwing Exception, the exception is as below

    "

    Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 217.33.210.17:443
       at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
       at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
       --- End of inner exception stack trace ---
       at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
       at System.Net.HttpWebRequest.GetRequestStream()"

    Thanks & Regards,
    Manjunath C Bhat,
    http://manjunathcbhat.blogspot.com
    http://manjunathcbhat.wordpress.com

    Tuesday, December 17, 2013 7:44 AM
  • That is indicative that your endpoint is wrong as it failed connecting to the remote server.  The specific error seems to indicate you're not using the right URL, right HTTP method or right port. 

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Tuesday, December 17, 2013 3:07 PM
    Moderator
  • Hi All,

    Thank you very much. I think the logic ultimately worked, but something wrong with the service i am trying to connect to. I will check with this and will come back asap.

    I might now need more help using a Web Service Task.

    THanks again.


    Thanks & Regards,
    Manjunath C Bhat,
    http://manjunathcbhat.blogspot.com
    http://manjunathcbhat.wordpress.com

    Tuesday, December 17, 2013 3:41 PM
  • Hi,

    Please post another thread on correct forum for more effective reply.

    Web Service Forum

    Regards,

    Thursday, December 19, 2013 8:25 AM
  • Hi All,

    I got my issue resolved. It was the proxy which was troubling me. I added below code and now I am able to download the data.

    myXmlhttp.Proxy = new WebProxy() { UseDefaultCredentials = true };
                myXmlhttp.Proxy = WebRequest.DefaultWebProxy;
                myXmlhttp.Credentials = new NetworkCredential("myUserName", "myPassword", "myDomain");
                myXmlhttp.Proxy.Credentials = new NetworkCredential("myUserName", "myPassword", "myDomain");

    Thank you all for your kind responses as always.


    Thanks & Regards,
    Manjunath C Bhat,
    http://manjunathcbhat.blogspot.com
    http://manjunathcbhat.wordpress.com

    Thursday, December 19, 2013 1:18 PM