Post XML with HttpWebRequest

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chakravarthy

    Post XML with HttpWebRequest


    I’m writing a windows application which intern talks to a web interface
    exposed at http://194.224.184.162/barceloDS/interface/xml . Now my problem
    is, this exposed interface will accept the XML String as parameter and
    returns data in XML Format. You can find the example of the XML Input, at
    http://194.224.184.162/barceloDS/interface/example, which explains in detail
    about the input XML String.

    I’m trying to execute the following code, which returning me null text,
    instead of some response. Correct me where I’m mistaken.

    string GetResp()
    {
    // This is the XML Input String to the exposed XML Interface
    string strReq = @"<?xml version=""1.0"" encoding=""UTF-8""
    ?><barceloDS_re quests><request type=""availabi lity list"" id=""1"">
    <session_id></session_id> <language_code> ING</language_code> <agency>
    <primary>888</primary> <secondary>88 </secondary> <detail>888</detail>
    <branch>1</branch> </agency> <contract></contract>
    <check_in_date> 20060801</check_in_date>
    <check_out_date >20060803</check_out_date> <location>
    <destination_co de>PMI</destination_cod e> <zone_code></zone_code> </location>
    <establishmen t> <code></code> <category></category> </establishment>
    <board_type_cod e></board_type_code > <occupancy> <adults>2</adults>
    <children>1</children> <rooms>1</rooms> </occupancy> </request>
    </barceloDS_reque sts>";

    HttpWebRequest hwrRequest = (HttpWebRequest )
    HttpWebRequest. Create("http://194.224.184.162/barceloDS/interface/xml");
    hwrRequest.Meth od="POST";
    hwrRequest.Cont entType = "text/xml";

    UTF8Encoding encoding = new UTF8Encoding();
    byte[] postBytes = encoding.GetByt es(strReq);

    hwrRequest.Cont entLength=postB ytes.Length;

    Stream postStream = hwrRequest.GetR equestStream();
    postStream.Writ e(postBytes,0,p ostBytes.Length );
    postStream.Clos e();

    HttpWebResponse hwrResponse = (HttpWebRespons e) hwrRequest.GetR esponse();
    Stream responseStream = hwrResponse.Get ResponseStream( );
    XmlTextReader xtrSmp = new XmlTextReader(r esponseStream);

    string strXm = xtrSmp.ReadInne rXml();
    xtrSmp.Close();
    hwrResponse.Clo se();

    return strXm;
    }

    Regards,
    --
    Every thing is perfect, as long as you share!!!
  • Hans Olav

    #2
    Re: Post XML with HttpWebRequest

    Hi.

    I would definitely use WebClient instead of the rather messy
    HttpWebRequest/Response.
    The WebClient class has a method called UploadData which takes a byte array
    and a method, and returns a resulting byte array. You can also access header
    info using the ResponseHeaders collection.

    'Hans Olav.


    Comment

    • Hans Olav

      #3
      Re: Post XML with HttpWebRequest

      Here's an example using WebClient. Usually UTF-8 is the standard encoding to
      use in both request and response from webservices, but it seems that this
      site is using iso-8859-1 encoding, so I've set up both the request and
      response bytes to be parsed using iso-8859-1. You can easily change this
      back to UTF-8 in the isoEncoding variable...

      XmlDocument Doc = new XmlDocument();
      WebClient Client = new WebClient();
      Encoding isoEncoding = Encoding.GetEnc oding("iso-8859-1");
      string url = http://194.224.184.162/barceloDS/interface/xml;
      string strReq =
      @"<?xml version=""1.0""
      encoding=""iso-8859-1""?><barceloDS _requests><requ est type=""availabi lity
      list"" id=""1"">
      <session_id></session_id> <language_code> ING</language_code> <agency>
      <primary>888</primary> <secondary>88 </secondary> <detail>888</detail>
      <branch>1</branch> </agency> <contract></contract>
      <check_in_date> 20060801</check_in_date>
      <check_out_date >20060803</check_out_date> <location>
      <destination_co de>PMI</destination_cod e> <zone_code></zone_code> </location>
      <establishmen t> <code></code> <category></category> </establishment>
      <board_type_cod e></board_type_code > <occupancy> <adults>2</adults>
      <children>1</children> <rooms>1</rooms> </occupancy> </request>
      </barceloDS_reque sts>";
      byte[] bytesReq = isoEncoding.Get Bytes(strReq);
      byte[] respBytes = Client.UploadDa ta(url, "POST", bytesReq);
      MemoryStream MemStream = new MemoryStream(re spBytes, false);
      using (StreamReader Reader = new StreamReader(Me mStream, isoEncoding))
      {
      Doc.Load(Reader );
      }


      Comment

      Working...