Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

How to send a xml file over HTTP and HTTPS protocol and get result back

i want to send xml file with userid and password over HTTPs and then send all other xml file on HTTP using POST method and get the response as a xml file back. in ASP.NET (with vb.net preferred)

The url to which i want to send my xml file is:http://www.hostelspoint.com/xml/xml.php exect xml file pettern is:

<?xml version="1.0" encoding="UTF-8"?>
<OTA_PingRQ xmlns="http://www.opentravel.org/OTA/2003/05"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05OTA_PingRQ.xsd"
      TimeStamp="2003-03-17T11:09:47-05:00"
      Target="Production" Version="1.001" PrimaryLangID="en"
      EchoToken="testtoken12">
  <EchoData>Hello</EchoData>
</OTA_PingRQ>

Upvotes: 1

Views: 7939

Answers (3)

user391318
user391318

Reputation: 61

Yes, you can do same thing using HTTPS protocol. You have to add this code before request:

System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
         {
             bool validationResult = true;

             //
             // policy code here ...
             //

             return validationResult;
         };

Upvotes: 1

Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

i don't know why u removed correct answer from here but yesterday i got correct answer here. and it is:- (can any one tell me how to do same with HTTPS protocol?)

    string targetUri = "http://www.hostelspoint.com/xml/xml.php";
    System.Xml.XmlDocument reqDoc = new System.Xml.XmlDocument();
    reqDoc.Load(Server.MapPath("~\\myfile.xml"));
    string formParameterName = "OTA_request";
    string xmlData = reqDoc.InnerXml;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri);
    string sendString = formParameterName + "=" + HttpUtility.UrlEncode(xmlData);
    byte[] byteStream;
    byteStream = System.Text.Encoding.UTF8.GetBytes(sendString);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = byteStream.LongLength;
    using (Stream writer = request.GetRequestStream())
    {
        writer.Write(byteStream, 0, (int)request.ContentLength);
        writer.Flush();
    }
    HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
    string respStr = "";
    if (request.HaveResponse)
    {
        if (resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.Accepted)
        {
            StreamReader respReader = new StreamReader(resp.GetResponseStream());
            respStr = respReader.ReadToEnd(); // get the xml result in the string object  
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(respStr);
            Label1.Text = doc.InnerXml.ToString();               
                 }
    } 

Upvotes: 2

marc_s
marc_s

Reputation: 754468

You should check out the WCF REST Starter Kit, and watch the screencast on HTTP Plain XML (POX) Services which explains step by step how to do just that - create a WCF REST service that will accept and process a plain XML stream.

All the WCF and WCF REST screencasts by Pluralsight are highly recommended! It's excellent material on how to get started and work with WCF.

In addition to that, the MSDN WCF Developer Center is your first point of contact for any questions or more information on WCF and WCF REST.

Upvotes: 2

Related Questions