Solved

Send XML to PHP

Posted on 2008-10-20
7
387 Views
Last Modified: 2013-12-17
How can I convert this VB code into C#

Public Function SendXML(sXMLFile As String, fNameValue As String) As MSXML2.ServerXMLHTTP
    On Error Resume Next
    Dim objsrvhttp As New MSXML2.ServerXMLHTTP
    Dim objXMLsend As New MSXML2.DOMDocument
    Dim objXMLReceive As New MSXML2.DOMDocument
    Dim currNode As IXMLDOMNode
    objXMLsend.async = False
   
    If sXMLFile = "" Then End
    objXMLsend.Load (sXMLFile)
   
    Dim fso As New Scripting.FileSystemObject
    fso.CopyFile sXMLFile, "C:\" & fNameValue & ".xml"  
   

    If objXMLsend.parseError.reason <> "" Then
        Print #m_filenum, "XML Error for file: " & fNameValue
        Print #m_filenum, objXMLsend.parseError.reason
        Close m_filenum
        Set SendXML = Null
        Exit Function
    End If
   
    Set currNode = objXMLsend.documentElement.childNodes.item(0)
    objsrvhttp.Open "POST", "url", False
    objsrvhttp.send objXMLsend
    Set objXMLReceive = objsrvhttp.responseXML
   
    Print #m_filenum, objsrvhttp.responseText
    Close m_filenum
    Set SendXML = objsrvhttp
   
End Function

0
Comment
Question by:sonashish
  • 4
  • 3
7 Comments
 
LVL 8

Accepted Solution

by:
mkosbie earned 500 total points
ID: 22760348
currNode and objXMLReceive don't appear to be used for anything in this function.  They go out of scope as soon as the function finishes executing.  Any reason you have them?  Also, I wasn't sure where m_filenum came from, so I made up a filestream for it.  Make sure the stream gets initialized at some point.
using System;

using System.Xml;

using System.IO;

using System.Net;
 

public class XMLExample

{

    private FileStream m_file; //NOTE: You MUST open this FileStream at some point or the function won't work

    private StreamWriter m_filewriter;
 

    public XMLExample()

    {

        //Example opening the streams

        this.m_file = new FileStream("C:\\yourfile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);

        this.m_fileWriter = new StreamWriter(m_file);

    }
 

    public HttpWebRequest SendXML(String sXMLFile, String fNameValue) {

        HttpWebRequest objsrvhttp = WebRequest.Create("url");

        XmlDocument objXMLsend = new XmlDocument();

        XmlDocument objXMLReceive = new XmlDocument();

        XmlNode currNode;
 

        if (sXMLFile = "") return;

        try

        {

            objXMLsend.Load(sXMLFile);

            File.Copy(sXMLFile, "C:\\" + fNameValue + ".xml");

        }

        catch (XmlException ex)

        {

            m_filewriter.WriteLine("XML error for file: " + fNameValue);

            m_filewriter.WriteLine(ex.Message);

            m_filewriter.Close();

            

            return null;

        }finally {}
 

        currNode = objXMLsend.DocumentElement.ChildNodes.Item(0);

        

        StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream());

        objsrvhttp.Method = "POST";

        objsrvhttp.ContentType = "applicatoin/x-www-form-encoded";

        sw.Write(objXMLsend);

        sw.Close();

        

        objsrvrcv = objsrvhttp.GetResponse();

        StreamReader sr = new StreamReader(objsrvhttp.GetResponse().GetResponseStream());

        String rsp = sr.ReadToEnd();
 

        objXMLReceive.LoadXml(rsp);

        m_filewriter.WriteLine(rsp);

        m_filewriter.Close();
 

        return objsrvhttp;

    }

}

Open in new window

0
 
LVL 2

Author Comment

by:sonashish
ID: 22760578
I am getting
Cannot implicitly convert type 'System.Net.WebRequest' to 'System.Net.HttpWebRequest'. An explicit conversion exists (are you missing a cast?)       
error on HttpWebRequest objsrvhttp = WebRequest.Create("url"); line.
0
 
LVL 8

Expert Comment

by:mkosbie
ID: 22760594
Stupid C#.....  Change the line to be:

HttpWebRequest objsrvhttp = (HttpWebRequest) WebRequest.Create("url");
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Author Comment

by:sonashish
ID: 22760679
Thanks.
Now I am getting
ProtocolViolationException (Cannot send a content-body with this verb-type.) at
StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream()); line.
0
 
LVL 8

Expert Comment

by:mkosbie
ID: 22760826
Sorry, default method for a request is GET.  Switch that line with the line below it (objsrvhttp.Method = "POST") and you should be fine:

        objsrvhttp.Method = "POST";
        StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream());

INSTEAD of:

        StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream());
        objsrvhttp.Method = "POST";
0
 
LVL 2

Author Comment

by:sonashish
ID: 22762816
Now I have a problem in objsrvhttp.GetRequestStream()

objsrvhttp.GetRequestStream()
{System.Net.ConnectStream}
    [System.Net.ConnectStream]: {System.Net.ConnectStream}
    base {System.MarshalByRefObject}: {System.Net.ConnectStream}
    CanRead: false
    CanSeek: false
    CanTimeout: true
    CanWrite: true
    Length: 'objsrvhttp.GetRequestStream().Length' threw an exception of type 'System.NotSupportedException'
    Position: 'objsrvhttp.GetRequestStream().Position' threw an exception of type 'System.NotSupportedException'
    ReadTimeout: 0x000493e0
    WriteTimeout: 0x000493e0

0
 
LVL 8

Expert Comment

by:mkosbie
ID: 22785663
Alright, I ran the code through a debugger this time and cleaned it up.  It works fine on my computer (I can instantiate an XMLExample and execute the SendXML method).  I didn't see any changes I made on the objsrvhttp.GetRequestStream() line, but maybe I did.  Try this version and report any errors.
using System;

using System.Xml;

using System.IO;

using System.Net;
 

public class XMLExample

{

    private FileStream m_file; //NOTE: You MUST open this FileStream at some point or the function won't work

    private StreamWriter m_filewriter;
 

    public XMLExample()

    {

        //Example opening the streams

        this.m_file = new FileStream("C:\\yourfile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);

        this.m_filewriter = new StreamWriter(m_file);

    }
 

    public HttpWebRequest SendXML(String sXMLFile, String fNameValue)

    {

        HttpWebRequest objsrvhttp = (HttpWebRequest)WebRequest.Create("URL");

        XmlDocument objXMLsend = new XmlDocument();

        XmlDocument objXMLReceive = new XmlDocument();

        XmlNode currNode;
 

        if (sXMLFile == "") return null;

        try

        {

            objXMLsend.Load(sXMLFile);

            File.Copy(sXMLFile, "C:\\" + fNameValue + ".xml", true);

        }

        catch (XmlException ex)

        {

            m_filewriter.WriteLine("XML error for file: " + fNameValue);

            m_filewriter.WriteLine(ex.Message);

            m_filewriter.Close();
 

            return null;

        }

        catch (Exception ex)

        {

            //All other errors

        }

        finally { }
 

        currNode = objXMLsend.DocumentElement.ChildNodes.Item(0);
 

        objsrvhttp.Method = "POST";

        StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream());

        objsrvhttp.ContentType = "application/x-www-form-encoded";

        sw.Write(objXMLsend);

        sw.Close();
 

        StreamReader sr = new StreamReader(objsrvhttp.GetResponse().GetResponseStream());

        String rsp = sr.ReadToEnd();
 

        try

        {

            objXMLReceive.LoadXml(rsp);

        }

        catch (XmlException ex)

        {

            //Xml response was not in proper format

        }

        catch (Exception ex)

        {

            //All other errors

        }

        finally {}
 

        m_filewriter.WriteLine(rsp);

        m_filewriter.Close();
 

        return objsrvhttp;

    }

}

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

757 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

22 Experts available now in Live!

Get 1:1 Help Now