473,409 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

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_requests><request type=""availability 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_code>PMI</destination_code> <zone_code></zone_code> </location>
<establishment> <code></code> <category></category> </establishment>
<board_type_code></board_type_code> <occupancy> <adults>2</adults>
<children>1</children> <rooms>1</rooms> </occupancy> </request>
</barceloDS_requests>";

HttpWebRequest hwrRequest = (HttpWebRequest)
HttpWebRequest.Create("http://194.224.184.162/barceloDS/interface/xml");
hwrRequest.Method="POST";
hwrRequest.ContentType = "text/xml";

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

hwrRequest.ContentLength=postBytes.Length;

Stream postStream = hwrRequest.GetRequestStream();
postStream.Write(postBytes,0,postBytes.Length);
postStream.Close();

HttpWebResponse hwrResponse = (HttpWebResponse) hwrRequest.GetResponse();
Stream responseStream = hwrResponse.GetResponseStream();
XmlTextReader xtrSmp = new XmlTextReader(responseStream);

string strXm = xtrSmp.ReadInnerXml();
xtrSmp.Close();
hwrResponse.Close();

return strXm;
}

Regards,
--
Every thing is perfect, as long as you share!!!
May 8 '06 #1
2 8177
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.
May 10 '06 #2
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.GetEncoding("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><request type=""availability
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_code>PMI</destination_code> <zone_code></zone_code> </location>
<establishment> <code></code> <category></category> </establishment>
<board_type_code></board_type_code> <occupancy> <adults>2</adults>
<children>1</children> <rooms>1</rooms> </occupancy> </request>
</barceloDS_requests>";
byte[] bytesReq = isoEncoding.GetBytes(strReq);
byte[] respBytes = Client.UploadData(url, "POST", bytesReq);
MemoryStream MemStream = new MemoryStream(respBytes, false);
using (StreamReader Reader = new StreamReader(MemStream, isoEncoding))
{
Doc.Load(Reader);
}
May 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not...
3
by: ME | last post by:
Hi; I am getting "Unhandled Exception: System.Net.WebException: The remote server returned an erro r: (401) Unauthorized." when I am trying to get a page via post. Code follows...
5
by: Tammy | last post by:
Hi, I have an aspx app which needs to post data to a form and read the response. I am confused on whether I should be using the get_url using "POST" method or the post_url using "GET" method. ...
3
by: Tom | last post by:
Hi. After I got some information from the user I need to POST it in XML format to the server NOT MY SERVER - a server at https://xxxx.xxx and to reveive the response. Please let me know...
2
by: Vishal | last post by:
Hello, I am posting some credit card info to the payment server via a similar code. My code is written in VB.NET but uses the same approach. At the end however I do a...
7
by: | last post by:
Hello, I would like to do the following from a asp.net button click: <form method="POST" action="https://www.1234.com/trans_center/gateway/direct.cgi"> <input type="hidden" name="Merchant"...
1
by: Dave Brown | last post by:
I am attempting to post to a url (https://FakeURL/logon.asp) using the HttpWebRequest class. The response for a succesful post will contain the html for the logon user's default page. We've...
3
by: JansenH | last post by:
We have implemented a 'HTTP Post' client in C# that posts Xml documents to a webserver. This is working fine if the post rate is one post for every 20 seconds. But if the post rate is increased to...
4
by: Natalia | last post by:
Hello, I need to provide the ability to post file and some form elements via our website (asp.net) to the third party website (asp page). On http://aspalliance.com/236#Page4 - I found great...
6
by: cethie | last post by:
In the code below, DoGet is working very stable. But DoPost throws an uncatcheable InvalidOperationException randomly. I am lost. Any pointers will be of immense help. /* Environment...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.