EDN Admin
Well-known member
c# post using HttpWebRequest.I want to post my XmlRequest with HttpWebRequest.Im using HttpWebRequest and response but İt doesnt work,I take an error.What is the problem I dont Understand because before Im using Httpwebrequest for other xmlrequest
and different url, it is work.Now I want to send my XmlRequest to my portal url with HttpWebRequest but I have an error<br/>
Can You Help me please?Why I can take a this error.
I wrote 2 method for post xmlrequest and using HttpWebRequest and MSXML but I can take an error.Im sending xmlrequest I can take statuscode is ok but
Error :
<?xml version="1.0" encoding="UTF-8"?><br/>
<XMLRequestErrors><br/>
<Error>Empty XML request</Error><br/>
</XMLRequestErrors>
First method:
public string[] GetCheckAvability(string userCode, string userName, string password, DateTime requestTime, string destinationID, DateTime checkIn, DateTime checkOut, string currency, string rateTYpe, int adult, int childreen, int age1, int age2)<br/>
{<br/>
string[] returnXMLValue;<br/>
string xml = "<?xml version="1.0" encoding="UTF-8"?>";<br/>
xml += "<XMLRequest>";<br/>
xml += "<RequestType>CheckAvailabilityRequest</RequestType>";<br/>
xml += "<RequestLogin>";<br/>
xml += "<AffiliateCode>" + userCode + "</AffiliateCode>";<br/>
xml += "<AffiliateUsername>" + userName + "</AffiliateUsername>";<br/>
xml += "<AffiliatePassword>" + password + "</AffiliatePassword>";<br/>
xml += "<AffRequestId>2</AffRequestId>";<br/>
xml += "<AffRequestTime>" + requestTime.ToString("yyyy-MM-ddThh:mm:ss") + "</AffRequestTime>";<br/>
xml += "</RequestLogin>";<br/>
xml += "<CheckAvailabilityCriteria>";<br/>
xml += "<DestinationId>" + destinationID + "</DestinationId>";<br/>
xml += " <HotelName />";<br/>
xml += "<HotelCode />";<br/>
xml += "<CheckIn>" + checkIn.ToString("yyyy-MM-dd") + "</CheckIn>";<br/>
xml += "<CheckOut>" + checkOut.ToString("yyyy-MM-dd") + "</CheckOut>";<br/>
xml += "<Currency>" + currency + "</Currency>";<br/>
xml += "<RateType>" + rateTYpe + "</RateType>";<br/>
xml += "<Rooms>";<br/>
xml += "<Room>";<br/>
xml += "<Adults>" + adult + "</Adults>";<br/>
xml += "<Children>" + childreen + "</Children>";<br/>
xml += "<ChildrenAges>";<br/>
xml += "<ChildAge>" + age1 + "</ChildAge>";<br/>
xml += "<ChildAge>" + age2 + "</ChildAge>";<br/>
xml += "</ChildrenAges>";<br/>
xml += "</Room>";<br/>
xml += "</Rooms>";<br/>
xml += " <OnRequest>1</OnRequest>";<br/>
xml += "</CheckAvailabilityCriteria>";<br/>
xml += "</XMLRequest>";<br/>
string url = "myportalurladdress";<br/>
<br/>
try <br/>
{
<br/>
MSXML2.ServerXMLHTTP xmlhttp = new MSXML2.ServerXMLHTTP();<br/>
MSXML2.DOMDocument objDom = new MSXML2.DOMDocument();<br/>
objDom.async = false;<br/>
objDom.loadXML(xml);<br/>
xmlhttp.open("POST", url, false, "", "");<br/>
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");<br/>
xmlhttp.send(objDom.xml);<br/>
returnXMLValue = new string[] { xml, xmlhttp.responseText };<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
<br/>
returnXMLValue = new string[] { xml, ex.Message };<br/>
}<br/>
<br/>
return returnXMLValue;<br/>
}
Second Method with HttpWebRequest:
public string[] GetCheckAvability2(string userCode, string userName, string password, DateTime requestTime, string destinationID, DateTime checkIn, DateTime checkOut, string currency, string rateTYpe, int adult, int childreen, int age1, int age2)<br/>
{<br/>
XmlDocument returnXMLValue = null;<br/>
string[] xmlResult;<br/>
string xml = "<?xml version="1.0" encoding="UTF-8"?>";<br/>
xml += "<XMLRequest>";<br/>
xml += "<RequestType>CheckAvailabilityRequest</RequestType>";<br/>
xml += "<RequestLogin>";<br/>
xml += "<AffiliateCode>" + userCode + "</AffiliateCode>";<br/>
xml += "<AffiliateUsername>" + userName + "</AffiliateUsername>";<br/>
xml += "<AffiliatePassword>" + password + "</AffiliatePassword>";<br/>
xml += "<AffRequestId>2</AffRequestId>";<br/>
xml += "<AffRequestTime>" + requestTime.ToString("yyyy-MM-ddThh:mm:ss") + "</AffRequestTime>";<br/>
xml += "</RequestLogin>";<br/>
xml += "<CheckAvailabilityCriteria>";<br/>
xml += "<DestinationId>" + destinationID + "</DestinationId>";<br/>
xml += " <HotelName />";<br/>
xml += "<HotelCode />";<br/>
xml += "<CheckIn>" + checkIn.ToString("yyyy-MM-dd") + "</CheckIn>";<br/>
xml += "<CheckOut>" + checkOut.ToString("yyyy-MM-dd") + "</CheckOut>";<br/>
xml += "<Currency>" + currency + "</Currency>";<br/>
xml += "<RateType>" + rateTYpe + "</RateType>";<br/>
xml += "<Rooms>";<br/>
xml += "<Room>";<br/>
xml += "<Adults>" + adult + "</Adults>";<br/>
xml += "<Children>" + childreen + "</Children>";<br/>
xml += "<ChildrenAges>";<br/>
xml += "<ChildAge>" + age1 + "</ChildAge>";<br/>
xml += "<ChildAge>" + age2 + "</ChildAge>";<br/>
xml += "</ChildrenAges>";<br/>
xml += "</Room>";<br/>
xml += "</Rooms>";<br/>
xml += " <OnRequest>1</OnRequest>";<br/>
xml += "</CheckAvailabilityCriteria>";<br/>
xml += "</XMLRequest>";<br/>
string url = "myportalurladdress";<br/>
<br/>
try<br/>
{<br/>
XmlDocument doc = new XmlDocument();<br/>
doc.LoadXml(xml);<br/>
<br/>
returnXMLValue = PostXMLTransaction(url, doc);<br/>
<br/>
xmlResult = new string[] { xml, returnXMLValue.InnerXml };<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
<br/>
xmlResult = new string[] { xml, ex.Message };<br/>
<br/>
}<br/>
<br/>
<br/>
return xmlResult;<br/>
}<br/>
<br/>
public XmlDocument PostXMLTransaction(string URL, XmlDocument Doc)<br/>
{<br/>
XmlDocument XMLResponse = null;<br/>
HttpWebRequest objHttpWebRequest;<br/>
HttpWebResponse objHttpWebResponse = null;<br/>
<br/>
Stream objRequestStream = null;<br/>
Stream objResponseStream = null;<br/>
XmlTextReader objXMLReader;<br/>
<br/>
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);<br/>
<br/>
try<br/>
{<br/>
byte[] bytes;<br/>
bytes = System.Text.Encoding.ASCII.GetBytes(Doc.InnerXml);<br/>
objHttpWebRequest.Method = "POST";<br/>
objHttpWebRequest.ContentLength = bytes.Length;<br/>
objHttpWebRequest.ContentType = "text/xml; encoding=utf-8";<br/>
objRequestStream = objHttpWebRequest.GetRequestStream();<br/>
objRequestStream.Write(bytes, 0, bytes.Length);<br/>
objRequestStream.Close();<br/>
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();<br/>
<br/>
if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)<br/>
{<br/>
<br/>
objResponseStream = objHttpWebResponse.GetResponseStream();<br/>
objXMLReader = new XmlTextReader(objResponseStream);<br/>
XmlDocument xmldoc = new XmlDocument();<br/>
xmldoc.Load(objXMLReader);<br/>
XMLResponse = xmldoc;<br/>
objXMLReader.Close();<br/>
}<br/>
objHttpWebResponse.Close();<br/>
objRequestStream.Close();<br/>
objResponseStream.Close();<br/>
objHttpWebResponse.Close();<br/>
<br/>
objXMLReader = null;<br/>
objRequestStream = null;<br/>
objResponseStream = null;<br/>
objHttpWebResponse = null;<br/>
objHttpWebRequest = null;<br/>
}<br/>
catch (WebException we)<br/>
{<br/>
throw new Exception(we.Message);<br/>
objRequestStream.Close();<br/>
objResponseStream.Close();<br/>
objHttpWebResponse.Close();<br/>
<br/>
objXMLReader = null;<br/>
objRequestStream = null;<br/>
objResponseStream = null;<br/>
objHttpWebResponse = null;<br/>
objHttpWebRequest = null;<br/>
<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
throw new Exception(ex.Message);<br/>
objRequestStream.Close();<br/>
objResponseStream.Close();<br/>
objHttpWebResponse.Close();<br/>
<br/>
objXMLReader = null;<br/>
objRequestStream = null;<br/>
objResponseStream = null;<br/>
objHttpWebResponse = null;<br/>
objHttpWebRequest = null;<br/>
}<br/>
<br/>
return XMLResponse;<br/>
<br/>
}
View the full article
and different url, it is work.Now I want to send my XmlRequest to my portal url with HttpWebRequest but I have an error<br/>
Can You Help me please?Why I can take a this error.
I wrote 2 method for post xmlrequest and using HttpWebRequest and MSXML but I can take an error.Im sending xmlrequest I can take statuscode is ok but
Error :
<?xml version="1.0" encoding="UTF-8"?><br/>
<XMLRequestErrors><br/>
<Error>Empty XML request</Error><br/>
</XMLRequestErrors>
First method:
public string[] GetCheckAvability(string userCode, string userName, string password, DateTime requestTime, string destinationID, DateTime checkIn, DateTime checkOut, string currency, string rateTYpe, int adult, int childreen, int age1, int age2)<br/>
{<br/>
string[] returnXMLValue;<br/>
string xml = "<?xml version="1.0" encoding="UTF-8"?>";<br/>
xml += "<XMLRequest>";<br/>
xml += "<RequestType>CheckAvailabilityRequest</RequestType>";<br/>
xml += "<RequestLogin>";<br/>
xml += "<AffiliateCode>" + userCode + "</AffiliateCode>";<br/>
xml += "<AffiliateUsername>" + userName + "</AffiliateUsername>";<br/>
xml += "<AffiliatePassword>" + password + "</AffiliatePassword>";<br/>
xml += "<AffRequestId>2</AffRequestId>";<br/>
xml += "<AffRequestTime>" + requestTime.ToString("yyyy-MM-ddThh:mm:ss") + "</AffRequestTime>";<br/>
xml += "</RequestLogin>";<br/>
xml += "<CheckAvailabilityCriteria>";<br/>
xml += "<DestinationId>" + destinationID + "</DestinationId>";<br/>
xml += " <HotelName />";<br/>
xml += "<HotelCode />";<br/>
xml += "<CheckIn>" + checkIn.ToString("yyyy-MM-dd") + "</CheckIn>";<br/>
xml += "<CheckOut>" + checkOut.ToString("yyyy-MM-dd") + "</CheckOut>";<br/>
xml += "<Currency>" + currency + "</Currency>";<br/>
xml += "<RateType>" + rateTYpe + "</RateType>";<br/>
xml += "<Rooms>";<br/>
xml += "<Room>";<br/>
xml += "<Adults>" + adult + "</Adults>";<br/>
xml += "<Children>" + childreen + "</Children>";<br/>
xml += "<ChildrenAges>";<br/>
xml += "<ChildAge>" + age1 + "</ChildAge>";<br/>
xml += "<ChildAge>" + age2 + "</ChildAge>";<br/>
xml += "</ChildrenAges>";<br/>
xml += "</Room>";<br/>
xml += "</Rooms>";<br/>
xml += " <OnRequest>1</OnRequest>";<br/>
xml += "</CheckAvailabilityCriteria>";<br/>
xml += "</XMLRequest>";<br/>
string url = "myportalurladdress";<br/>
<br/>
try <br/>
{
<br/>
MSXML2.ServerXMLHTTP xmlhttp = new MSXML2.ServerXMLHTTP();<br/>
MSXML2.DOMDocument objDom = new MSXML2.DOMDocument();<br/>
objDom.async = false;<br/>
objDom.loadXML(xml);<br/>
xmlhttp.open("POST", url, false, "", "");<br/>
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");<br/>
xmlhttp.send(objDom.xml);<br/>
returnXMLValue = new string[] { xml, xmlhttp.responseText };<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
<br/>
returnXMLValue = new string[] { xml, ex.Message };<br/>
}<br/>
<br/>
return returnXMLValue;<br/>
}
Second Method with HttpWebRequest:
public string[] GetCheckAvability2(string userCode, string userName, string password, DateTime requestTime, string destinationID, DateTime checkIn, DateTime checkOut, string currency, string rateTYpe, int adult, int childreen, int age1, int age2)<br/>
{<br/>
XmlDocument returnXMLValue = null;<br/>
string[] xmlResult;<br/>
string xml = "<?xml version="1.0" encoding="UTF-8"?>";<br/>
xml += "<XMLRequest>";<br/>
xml += "<RequestType>CheckAvailabilityRequest</RequestType>";<br/>
xml += "<RequestLogin>";<br/>
xml += "<AffiliateCode>" + userCode + "</AffiliateCode>";<br/>
xml += "<AffiliateUsername>" + userName + "</AffiliateUsername>";<br/>
xml += "<AffiliatePassword>" + password + "</AffiliatePassword>";<br/>
xml += "<AffRequestId>2</AffRequestId>";<br/>
xml += "<AffRequestTime>" + requestTime.ToString("yyyy-MM-ddThh:mm:ss") + "</AffRequestTime>";<br/>
xml += "</RequestLogin>";<br/>
xml += "<CheckAvailabilityCriteria>";<br/>
xml += "<DestinationId>" + destinationID + "</DestinationId>";<br/>
xml += " <HotelName />";<br/>
xml += "<HotelCode />";<br/>
xml += "<CheckIn>" + checkIn.ToString("yyyy-MM-dd") + "</CheckIn>";<br/>
xml += "<CheckOut>" + checkOut.ToString("yyyy-MM-dd") + "</CheckOut>";<br/>
xml += "<Currency>" + currency + "</Currency>";<br/>
xml += "<RateType>" + rateTYpe + "</RateType>";<br/>
xml += "<Rooms>";<br/>
xml += "<Room>";<br/>
xml += "<Adults>" + adult + "</Adults>";<br/>
xml += "<Children>" + childreen + "</Children>";<br/>
xml += "<ChildrenAges>";<br/>
xml += "<ChildAge>" + age1 + "</ChildAge>";<br/>
xml += "<ChildAge>" + age2 + "</ChildAge>";<br/>
xml += "</ChildrenAges>";<br/>
xml += "</Room>";<br/>
xml += "</Rooms>";<br/>
xml += " <OnRequest>1</OnRequest>";<br/>
xml += "</CheckAvailabilityCriteria>";<br/>
xml += "</XMLRequest>";<br/>
string url = "myportalurladdress";<br/>
<br/>
try<br/>
{<br/>
XmlDocument doc = new XmlDocument();<br/>
doc.LoadXml(xml);<br/>
<br/>
returnXMLValue = PostXMLTransaction(url, doc);<br/>
<br/>
xmlResult = new string[] { xml, returnXMLValue.InnerXml };<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
<br/>
xmlResult = new string[] { xml, ex.Message };<br/>
<br/>
}<br/>
<br/>
<br/>
return xmlResult;<br/>
}<br/>
<br/>
public XmlDocument PostXMLTransaction(string URL, XmlDocument Doc)<br/>
{<br/>
XmlDocument XMLResponse = null;<br/>
HttpWebRequest objHttpWebRequest;<br/>
HttpWebResponse objHttpWebResponse = null;<br/>
<br/>
Stream objRequestStream = null;<br/>
Stream objResponseStream = null;<br/>
XmlTextReader objXMLReader;<br/>
<br/>
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);<br/>
<br/>
try<br/>
{<br/>
byte[] bytes;<br/>
bytes = System.Text.Encoding.ASCII.GetBytes(Doc.InnerXml);<br/>
objHttpWebRequest.Method = "POST";<br/>
objHttpWebRequest.ContentLength = bytes.Length;<br/>
objHttpWebRequest.ContentType = "text/xml; encoding=utf-8";<br/>
objRequestStream = objHttpWebRequest.GetRequestStream();<br/>
objRequestStream.Write(bytes, 0, bytes.Length);<br/>
objRequestStream.Close();<br/>
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();<br/>
<br/>
if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)<br/>
{<br/>
<br/>
objResponseStream = objHttpWebResponse.GetResponseStream();<br/>
objXMLReader = new XmlTextReader(objResponseStream);<br/>
XmlDocument xmldoc = new XmlDocument();<br/>
xmldoc.Load(objXMLReader);<br/>
XMLResponse = xmldoc;<br/>
objXMLReader.Close();<br/>
}<br/>
objHttpWebResponse.Close();<br/>
objRequestStream.Close();<br/>
objResponseStream.Close();<br/>
objHttpWebResponse.Close();<br/>
<br/>
objXMLReader = null;<br/>
objRequestStream = null;<br/>
objResponseStream = null;<br/>
objHttpWebResponse = null;<br/>
objHttpWebRequest = null;<br/>
}<br/>
catch (WebException we)<br/>
{<br/>
throw new Exception(we.Message);<br/>
objRequestStream.Close();<br/>
objResponseStream.Close();<br/>
objHttpWebResponse.Close();<br/>
<br/>
objXMLReader = null;<br/>
objRequestStream = null;<br/>
objResponseStream = null;<br/>
objHttpWebResponse = null;<br/>
objHttpWebRequest = null;<br/>
<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
throw new Exception(ex.Message);<br/>
objRequestStream.Close();<br/>
objResponseStream.Close();<br/>
objHttpWebResponse.Close();<br/>
<br/>
objXMLReader = null;<br/>
objRequestStream = null;<br/>
objResponseStream = null;<br/>
objHttpWebResponse = null;<br/>
objHttpWebRequest = null;<br/>
}<br/>
<br/>
return XMLResponse;<br/>
<br/>
}
View the full article