locked
How to send and receive xml data via HttpWebRequest/HttpWebResponse in windows forms applications.

    Question

  • I am having hard time to post xml data to my web site (or to aspx page) which is running on IIS server.

    I am trying to post the xml string from windows form application.
    I am using “HttpWebRequest” class and reading the response using “HttpWebResponse”.
     

    My code works fine when I send plain text data, By setting 

    request.ContentType=@"application/x-www-form-urlencoded".


    When I try to send xml string I get 500-server internal error.
    I changed the request.ContentType= "text/xml;charset=\"utf-8\"";

    now error disappears. But when I debug my aspx page in web site,

    I don’t see any values coming in the request object?

    I mean there is no field called “xmldata” in request object.

    Can any body point to me how to set all propertys of HttpWebRequest to send xml data.
     

    Here is my code sample.

    HttpWebRequest request;
    postData="xmldata=" + inputXMLstring;
      
     

      request = (HttpWebRequest)WebRequest.Create (URI.AbsoluteUri);
       request.Method = "POST";
       ASCIIEncoding encoding = new ASCIIEncoding();
       byte[] body = encoding.GetBytes(postData);
       //request.ContentType=@"application/x-www-form-urlencoded";
       request.ContentType= @"application/x-qbxm";
       request.ContentLength=body.Length;
       Stream newStream = request.GetRequestStream();
       newStream.Write(body,0,body.Length);

      
       //request.MaximumAutomaticRedirections = 4;
       //request.MaximumResponseHeadersLength = 4;
       //request.AllowAutoRedirect =true;
       //request.Timeout = 300000;

       //request.ContentType=@"application/x-www-form-urlencoded";
       //request.ContentType= "text/xml;charset=\"utf-8\"";
       //request.ContentType = "application/x-qbxm";


       HttpWebResponse response = (HttpWebResponse) request.GetResponse();
       Stream receiveStream = myResp.GetResponseStream();

       StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

       textBox1.Text = readStream.ReadToEnd ();

    I tried all type of setting. But could not able to make it run. I am new to this HttpWebRequest/HttpWebResponse objects.

    Any help will be appreciated.

    Thanks,
    GS


     

     

    Friday, March 17, 2006 4:25 PM

Answers

  • First using Name=Value is a wrong thing to do.
    What we are doing is to post the XML as it as ---Not as a form post.
    We are using POST verb but this is not a form encoded post what we are saying is

    POST this data
    The ContentType = text/xml and it is UTF8 encoded

    On the receiving side in ASPX don't expect the data to arrive as a form posting.
    Use the request.read to read bytes of the request stream as it arrives.

    then get back the XML.

    the other issue is that you should *never* treat XML as a string. XML can contain leading byte markers and it is best to treat XML as data. Unless you know specifically that it does not contain leading byte markers - but you never know. Particularly if you
    serialize the XML document the XML object model can put the leading byte markers.

    Tuesday, March 21, 2006 7:34 PM
    Moderator

All replies

  • The form encoding means you should have
    name=value type string and that has to be url encoded.
    So just appending the xml string won't do.

    You don't need the form encoding. Since XML technically can contain
    unicode the best way is to treat it like data not as a string

    This is what I would recommend

    1. Get the bytes from the XML document as byte array.
    2. Then set the context type to be text/xml charset=utf8
    Please note that the charset should match the encoding you are using to
    coinvert XML to bytes
    3. Send the request
    4. On the ASPX page read the content type
    and load the document from the request bytes

    Sunday, March 19, 2006 7:53 PM
    Moderator
  • Hi Durgaprasad Gorti,

    Thanks for your reply.

    I tried as you mentioned in you reply, still I don’t see any xml string on target  web site.

    Here is the code sample, i am trying to make it work.

       HttpWebRequest request;

       postData= @"XMLDATA=<?xml version='1.0'?><ATOMIC_CHANGES><SECURITY  index = '0' status= 'Restricted' trlcomments= 'testsdfsdf'/></ATOMIC_CHANGES>";

       request = (HttpWebRequest)WebRequest.Create (URI.AbsoluteUri);
       request.Method = "POST";
       UTF8Encoding  encoding = new UTF8Encoding();
       byte[] body = encoding.GetBytes(postData);
      
       request.ContentType= "text/xml charset=utf8";
       request.ContentLength=body.Length;
       Stream newStream = request.GetRequestStream();
       newStream.Write(body,0,body.Length);
       return request;

    I am posting this data to target.aspx.  I am not getting any values in target.aspx page.
    Please let me know where I am doing wrong. Once again thanks for reply.
    Thanks,
    Gs

     

     

     

     

    Tuesday, March 21, 2006 4:14 PM
  • The first thing I would add before debugging any further is a call to Close() on your newStream object before leaving the function.  You should always close the streams and HttpWebResponse objects that you work with.  Calling close is the standard practice for these objects and will avoid many headaches later on...

    If that doesn't fix your problem, then please enable trace logging and send the output from that logging (remove private information before sending).  See http://blogs.msdn.com/dgorti/archive/2005/09/18/471003.aspx for information on trace logging.

    Tuesday, March 21, 2006 6:06 PM
    Moderator
  •  

    Thanks for your suggestions,
    We are using .Net Framework 1.1 I can't able to trace the application.
    If you have some sample code (to sends/recives xml data) which  run on your system, please send me I will try.
    I am able to get the xml data from my web site, but If send it never delivers to the web site.

    Thanks for your quick reply.

    Tuesday, March 21, 2006 7:19 PM
  • First using Name=Value is a wrong thing to do.
    What we are doing is to post the XML as it as ---Not as a form post.
    We are using POST verb but this is not a form encoded post what we are saying is

    POST this data
    The ContentType = text/xml and it is UTF8 encoded

    On the receiving side in ASPX don't expect the data to arrive as a form posting.
    Use the request.read to read bytes of the request stream as it arrives.

    then get back the XML.

    the other issue is that you should *never* treat XML as a string. XML can contain leading byte markers and it is best to treat XML as data. Unless you know specifically that it does not contain leading byte markers - but you never know. Particularly if you
    serialize the XML document the XML object model can put the leading byte markers.

    Tuesday, March 21, 2006 7:34 PM
    Moderator
  • Thanks for your Help, I appreciate your time.

    Yes it works...


    I was expecting xml string will come in name value collection in .aspx page and all this time I was looking in the form field.


    When I did Request [“xmldata”] on the target .aspx nothing was there!


    Last question :)  Can you please let me know (if you have some time) why XML  will not transmit in name value collection?


    Thanks,
    GS

     

     

     

     

    Tuesday, March 21, 2006 8:29 PM
  • Becuase name=value must be really URL encoded
    Thats what you are saying when you set the content type

    application/x-www-form-urlencoded

    That means the format of the data is name=value&name=value
    but the data is encoded using URL encoding. POST will work if
    1) You set the content type as above
    2) Set the content length
    3) Trasform the xml string to urlencoding
    4) then do the name=value [after url encoding]

     

    Wednesday, March 22, 2006 2:35 AM
    Moderator