Home > .NET Development > XML over HTTP - TRAVELfusion

XML over HTTP - TRAVELfusion



Hi guys,



I am working on an travel website that integrates with the TRAVELfusion API. They provide examples in Java and PHP, but I need to implement it using C#. I'm not to sure what is the best way to approach this. If anyone could give me some pointers I'll appreciate it.



Thanks.

Jo.

    
Guest


Will I need to do something like below?




c# Code:
















Original
- c# Code


  1.    System.Net.HttpWebRequest httpRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.C  reate("http://www.mayosoftware.com");
  2.             httpRequest.Method = "POST";
  3.             httpRequest.ContentType = "text/xml";
  4.  
  5.             System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(httpRequest.GetRequestStr  eam(),
  6.             System.Text.Encoding.UTF8);
  7.            
  8.             xmlWriter.WriteStartDocument();
  9.             xmlWriter.WriteStartElement("request");
  10.             xmlWriter.WriteElementString("store_id", store_id);
  11.             xmlWriter.WriteElementString("api_token", api_token);
  12.             xmlWriter.WriteStartElement("purchase");
  13.             xmlWriter.WriteElementString("order_id", System.Guid.NewGuid().ToString());
  14.             xmlWriter.WriteElementString("amount", amount);
  15.             xmlWriter.WriteElementString("pan",CCNumberBox.Text.Trim());
  16.             xmlWriter.WriteElementString("expdate",MonthList.SelectedValue + YearList.SelectedValue);
  17.             xmlWriter.WriteElementString("crypt_type", crypt);
  18.             xmlWriter.WriteEndElement();
  19.             xmlWriter.WriteEndElement();
  20.             xmlWriter.WriteEndDocument();
  21.             xmlWriter.Close();
  22.  
  23.                        
  24.             HttpWebResponse httpResponse = (HttpWebResponse)
  25.             httpRequest.GetResponse();
  26.        
  27.             XmlDocument responseXML = new XmlDocument();
  28.             responseXML.load(httpResponse.GetResponseStream())  ; 



Was this answer helpful ? Yes No   
Guest


Can someone please move this thread to the .Net forum.



Thanks.

Jo.

Was this answer helpful ? Yes No   
Guest


Quote:
Originally Posted by D.O.M.I.N.A.T.O.R
Can someone please move this thread to the .Net forum.



Thanks.

Jo.
done.

Was this answer helpful ? Yes No   
Guest


as for your question, I'm not sure what you're asking.

the code you posted should post XML data to remote website

and the syntax look fine. what's the problem?

Was this answer helpful ? Yes No   
Guest


Quote:
Originally Posted by Shadow Wizard
as for your question, I'm not sure what you're asking.

the code you posted should post XML data to remote website

and the syntax look fine. what's the problem?




Thanks for that, but I'm getting this error on line 28.




Code:


System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.







Code:


Line 28:                 responseXML.Load(httpResponse.GetResponseStream())  ;






The XML looks fine to me, see below:




Code:


<CommandList>
<Login>
<Username>User</Username>
<Password>Pass</Password>
</Login>
</CommandList>






Any help will be appreciated.

Was this answer helpful ? Yes No   
Guest


I believe that any XML data must begin with this line:


Code:


<?xml version="1.0"?>




otherwise it's not recognized as XML at all...

Was this answer helpful ? Yes No   
Guest


Quote:
Originally Posted by Shadow Wizard
I believe that any XML data must begin with this line:


Code:


<?xml version="1.0"?>




otherwise it's not recognized as XML at all...




Shadow, I have used fiddler (HTTP debugging proxy) to see what gets posted, and I found the following:




Code:


?<CommandList><Login><Username>User</Username><Password>Pass</Password></Login></CommandList>






Dont know if the ? needs to be there, and if that might be causing the error. Please let me know if you have any suggestions.



Thank you for your time.



Jo.

--

Was this answer helpful ? Yes No   
Guest


I am trying to submit an Xml requests to a remote website as the body of an HTTP request, and receive a response. Below is my code:






c# Code:
















Original
- c# Code


  1. HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://www.testUrl.com/Xml");
  2. httpRequest.Method = "POST";
  3. httpRequest.ContentType = "text/xml";
  4.  
  5. XmlTextWriter xmlWriter = new XmlTextWriter(httpRequest.GetRequestStream(), System.Text.Encoding.UTF8);
  6.  
  7. xmlWriter.WriteStartElement("CommandList");
  8. xmlWriter.WriteStartElement("Login");
  9. xmlWriter.WriteElementString("Username", "User");
  10. xmlWriter.WriteElementString("Password", "Pass");
  11. xmlWriter.WriteEndElement();
  12. xmlWriter.WriteFullEndElement();
  13. xmlWriter.Close();
  14.                            
  15. HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
  16.  
  17. XmlDocument responseXML = new XmlDocument();
  18. responseXML.Load(httpResponse.GetResponseStream())  ;






This does not work for me. Please let me know if anyone has any ideas.



Thanks.

J.

Was this answer helpful ? Yes No   
Guest


Quote:
Originally Posted by D.O.M.I.N.A.T.O.R
I am trying to submit an Xml requests to a remote website as the body of an HTTP request, and receive a response. Below is my code:



-- SNIP --



This does not work for me. Please let me know if anyone has any ideas.



Thanks.

J.




Hi D.O.M.I.N.A.T.O.R,



Someone pointed this in my direction, and I've created a basic little program which will essentially do what you want. It's a little long winded, but well commented. Hopefully this will be a step in the right direction for you




C# Code:
















Original
- C# Code


  1. /*
  2. * C# TravelFusion Example
  3. *
  4. * User: craig
  5. * Date: 06/03/2007
  6. * Time: 15:30
  7. *
  8. * This Application will perform a simple 'Login' Command,
  9. * it will detect failures such as invalid Username / Password
  10. * and return them to the person using it. Otherwise it will display
  11. * the XML received from the Server for later Parsing.
  12. *
  13. */
  14. using System;
  15. using System.IO;
  16. using System.Xml;
  17. using System.Net;
  18. using System.Text;
  19. using System.Net.Sockets;
  20. using System.Collections;
  21.  
  22. namespace TF
  23. {   
  24.     class MainClass
  25.     {
  26.  
  27.         int port = 80;
  28.         String host = "(URL address blocked: See forum rules)";
  29.         String username = "UserName";
  30.         String password = "Password";
  31.        
  32.         public static void Main(string[] args)
  33.         {
  34.             MainClass main = new MainClass();
  35.             main.run();
  36.             Console.ReadKey();
  37.         }
  38.        
  39.         private void generateXml(XmlTextWriter writer) {
  40.             writer.Formatting = Formatting.Indented;
  41.  
  42.             writer.WriteStartElement("CommandList");
  43.                 writer.WriteStartElement("Login", null);
  44.                     writer.WriteElementString("Username", this.username);
  45.                     writer.WriteElementString("Password", this.password);
  46.                 writer.WriteEndElement();
  47.             writer.WriteEndElement();
  48.         }
  49.                
  50.         public void run() {
  51.             // Create a MemoryStream for the XmlTextWriter to dump into
  52.             // (We need .Length to send as a Content-Length)
  53.             MemoryStream s = new MemoryStream();
  54.            
  55.             // Prepare the XmlTextWriter with our MemoryStream
  56.             XmlTextWriter writer = new XmlTextWriter(s, Encoding.ASCII);
  57.            
  58.             // Call our Xml Generator..
  59.             this.generateXml(writer);
  60.            
  61.             // Flush the Xml Generated into our stream.
  62.             writer.Flush();
  63.            
  64.            
  65.             // Create the Socket..
  66.             TcpClient socket = new TcpClient(this.host, this.port);
  67.        
  68.             // Grab the network stream, and send the HTTP Headers..
  69.             NetworkStream httpStream = socket.GetStream();
  70.             sendString(httpStream, "POST /Xml HTTP/1.0\r\n");
  71.             sendString(httpStream, "Content-Type: text/xml\r\n");
  72.            
  73.             // This is a pre-requisite for the way we're sending Xml..
  74.             sendString(httpStream, "Content-Length: " + s.Length + "\r\n");
  75.             sendString(httpStream, "\r\n");
  76.            
  77.             // Pull our Xml out the MemoryStream, and send it.
  78.             sendString(httpStream, System.Text.Encoding.ASCII.GetString(s.GetBuffer()  ));
  79.            
  80.             // Send the following to let The web server know we're finished.
  81.             sendString(httpStream, "\r\n\r\n");
  82.            
  83.             // Our MemoryStream is no longer required, close it here.
  84.             writer.Close();
  85.            
  86.             // Prepare a Read buffer, as well as StringBuilder to receive the response..
  87.             byte[] readBuffer = new Byte[2048];
  88.             StringBuilder complete = new StringBuilder();
  89.             int numBytes = 0;
  90.            
  91.             if (httpStream.CanRead) {
  92.                 do {
  93.                     // Pull the Response into the Builder..    
  94.                     numBytes = httpStream.Read(readBuffer, 0, readBuffer.Length);
  95.                     complete.AppendFormat("{0}", Encoding.UTF8.GetString(readBuffer, 0, numBytes));
  96.                 } while (httpStream.DataAvailable);
  97.             } else {
  98.                 // Premature Closing of Socket?
  99.                 return;
  100.             }
  101.                
  102.             // Strip Headers and Load the response into a String..
  103.             String Xml = complete.ToString().Substring(complete.ToString()IndexOf("\r\n\r\n") + 4);
  104.            
  105.             // Create a new MemoryStream to Store the Response Xml so it can be read using XmlTextReader           
  106.             MemoryStream r = new MemoryStream(ASCIIEncoding.Default.GetBytes(Xml))
  107.            
  108.             // Perform the reading..
  109.             XmlTextReader reader = new XmlTextReader(r);
  110.             while (reader.Read()) {
  111.                 switch (reader.NodeType)
  112.                 {
  113.                     case XmlNodeType.Element:
  114.                         // We have a tag, Verify it's what we wanted!
  115.                         if (reader.Name == "CommandExecutionFailure") {
  116.                             // Execution of the Command Failed, Find the original Tag..
  117.                             reader.ReadToFollowing("Login");
  118.                            
  119.                             // Then the error text attribute..
  120.                             while (reader.MoveToNextAttribute()) {
  121.                                 if (reader.Name == "etext") {
  122.                                     Console.WriteLine("Login Failed: " + reader.Value);
  123.                                 }
  124.                             }
  125.                            
  126.                             // Bail out here.
  127.                             Console.WriteLine("Press any key to continue..");
  128.                             Console.ReadKey();
  129.                             return;
  130.                         }
  131.                         break;
  132.                     // case ...:
  133.                 }
  134.             }
  135.             // If we get to here, an error hasn't been found.
  136.             Console.WriteLine(Xml);
  137.             Console.WriteLine("Press any key to continue..");
  138.             Console.ReadKey();
  139.         }
  140.        
  141.         private void sendString(NetworkStream httpStream, String text) {
  142.             byte[] data = new Byte[2084];
  143.             data = System.Text.Encoding.UTF8.GetBytes(text);
  144.             httpStream.Write(data, 0, data.Length);
  145.         }
  146.     }
  147. }






Remember to change the 'host' variable (forum rules will block it), you may also want to consider reworking the code a little to suite your needs more.



--

Craig

Was this answer helpful ? Yes No   
Guest
 
 
Home - About Infoqu - Contact - Privacy Statement - Link to Infoqu - Bookmark Infoqu

Copyright 2007-2010 by Infoqu. All rights reserved