Sending XML via HttpWebRequest

Discussion in 'ASP .Net' started by BuddyWork, Sep 29, 2004.

  1. BuddyWork

    BuddyWork Guest

    Hello,

    I want to send a XML message (string) to an ASP.net page,
    I don't want to use WebService because the ASP.net page
    has been developed by an external company, can you please
    point me to the correct location for examples on how to
    send XML messages to an ASP.net page using C#.

    Thanks,
     
    BuddyWork, Sep 29, 2004
    #1
    1. Advertising

  2. BuddyWork

    Hermit Dave Guest

    you want to send xml to aspx page but you do not want to use webservice.
    Well in anycase if you want to post the data to a webpage you need to use
    either QueryString or Form.
    With XML i am afraid it can get pretty long and you could run out of max
    querystring length.
    in that case just htmlencode the xml and pass it as a form param.
    Though to post to another aspx page (if you are using aspx as well) you will
    have to rely on html form rather than server side form (so remove
    runat=server from form in html view of designer)

    --

    Regards,

    Hermit Dave
    (http://hdave.blogspot.com)
    "BuddyWork" <> wrote in message
    news:368e01c4a60c$4f3ccea0$...
    > Hello,
    >
    > I want to send a XML message (string) to an ASP.net page,
    > I don't want to use WebService because the ASP.net page
    > has been developed by an external company, can you please
    > point me to the correct location for examples on how to
    > send XML messages to an ASP.net page using C#.
    >
    > Thanks,
     
    Hermit Dave, Sep 29, 2004
    #2
    1. Advertising

  3. BuddyWork

    Craig Guest

    I think something like this might work for you:


    /// <summary>
    /// Sends an xml document over http, and returns the xml server response
    /// </summary
    public XmlDocument SubmitDocument(XmlDocument xDoc, string URL)
    {
    try
    {
    // get the data from the xml document into a byte stream
    Byte[] bdata = System.Text.Encoding.ASCII.GetBytes(xDoc.OuterXml);
    // instantiate a web client
    System.Net.WebClient wc = new System.Net.WebClient();
    Byte[] bresp;
    // add appropriate headers
    wc.Headers.Add("Content-Type","text/xml");
    // send data to server, and wait for a response
    bresp = wc.UploadData(URL, bdata);
    // read the response
    string resp = System.Text.Encoding.ASCII.GetString(bresp);
    XmlDocument xresp = new XmlDocument();
    xresp.LoadXml(resp);
    // return the xml document response from the server
    return xresp;
    }
    catch
    {
    // your error handler
    SystemError();
    }

    }


    "BuddyWork" <> wrote in message
    news:368e01c4a60c$4f3ccea0$...
    > Hello,
    >
    > I want to send a XML message (string) to an ASP.net page,
    > I don't want to use WebService because the ASP.net page
    > has been developed by an external company, can you please
    > point me to the correct location for examples on how to
    > send XML messages to an ASP.net page using C#.
    >
    > Thanks,
     
    Craig, Sep 29, 2004
    #3
  4. BuddyWork

    Joerg Jooss Guest

    Craig wrote:
    > I think something like this might work for you:
    >
    >
    > /// <summary>
    > /// Sends an xml document over http, and returns the xml server
    > response /// </summary
    > public XmlDocument SubmitDocument(XmlDocument xDoc, string URL)
    > {
    > try
    > {
    > // get the data from the xml document into a byte stream
    > Byte[] bdata = System.Text.Encoding.ASCII.GetBytes(xDoc.OuterXml);
    > // instantiate a web client
    > System.Net.WebClient wc = new System.Net.WebClient();
    > Byte[] bresp;
    > // add appropriate headers
    > wc.Headers.Add("Content-Type","text/xml");
    > // send data to server, and wait for a response
    > bresp = wc.UploadData(URL, bdata);
    > // read the response
    > string resp = System.Text.Encoding.ASCII.GetString(bresp);
    > XmlDocument xresp = new XmlDocument();
    > xresp.LoadXml(resp);
    > // return the xml document response from the server
    > return xresp;
    > }
    > catch
    > {
    > // your error handler
    > SystemError();
    > }
    >
    > }


    Just a note: Don't use ACSII encoding with XML. Use the specific encoding
    your XML documents use, or UTF-8 as fallback. No non-7 bit character will
    survive this torture otherwise ;-)

    Cheers,

    --
    Joerg Jooss
     
    Joerg Jooss, Sep 30, 2004
    #4
  5. BuddyWork

    Craig Guest

    "Joerg Jooss" <> wrote in message
    news:...
    > Just a note: Don't use ACSII encoding with XML. Use the specific encoding
    > your XML documents use, or UTF-8 as fallback. No non-7 bit character will
    > survive this torture otherwise ;-)
    >
    > Cheers,
    >
    > --
    > Joerg Jooss
    >
    >


    You are, of course, correct, and thank you for pointing out my error.
    That's what I get for throwing something together in a hurry.

    Cheers,
    Craig
     
    Craig, Sep 30, 2004
    #5
  6. BuddyWork

    Joerg Jooss Guest

    Craig wrote:
    > "Joerg Jooss" <> wrote in message
    > news:...
    >> Just a note: Don't use ACSII encoding with XML. Use the specific
    >> encoding your XML documents use, or UTF-8 as fallback. No non-7 bit
    >> character will survive this torture otherwise ;-)
    >>
    >> Cheers,
    >>
    >> --
    >> Joerg Jooss
    >>
    >>

    >
    > You are, of course, correct, and thank you for pointing out my error.
    > That's what I get for throwing something together in a hurry.


    Never mind. Unfortunately, there are quite a few code samples on the web
    employing this dangerous practice :-S

    Cheers,

    --
    Joerg Jooss
     
    Joerg Jooss, Sep 30, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFydmlu?=

    httpWebRequest not sending client Certificate

    =?Utf-8?B?TWFydmlu?=, Aug 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,735
    Jens Christian Mikkelsen
    Aug 8, 2004
  2. John A Grandy

    submit form via HttpWebRequest or WebClient

    John A Grandy, Aug 24, 2004, in forum: ASP .Net
    Replies:
    14
    Views:
    18,730
    Joerg Jooss
    Aug 26, 2004
  3. Roshawn Dawson

    Sending an xml file in HttpWebRequest

    Roshawn Dawson, Sep 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,940
    Roshawn Dawson
    Sep 21, 2005
  4. =?Utf-8?B?RGF2ZSBCcm93bg==?=

    HttpWebRequest POST result is not the same as POST via ServerXMLHT

    =?Utf-8?B?RGF2ZSBCcm93bg==?=, Nov 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,166
    Joerg Jooss
    Nov 10, 2005
  5. Lenster
    Replies:
    4
    Views:
    1,284
    bruce barker
    Dec 4, 2008
Loading...

Share This Page