如何通过 HTTP 和 HTTPS 协议发送 xml 文件并获取结果

发布于 2024-08-16 04:10:43 字数 747 浏览 4 评论 0原文

我想通过 HTTP 发送带有用户 ID 和密码的 xml 文件,然后使用 POST 方法在 HTTP 上发送所有其他 xml 文件,并以 xml 文件形式获取响应。在 ASP.NET 中(首选 vb.net)

我想要将 xml 文件发送到的 url 是:http://www.hostelspoint.com/xml/xml.php 执行 xml 文件模式是:

<?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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眼睛不忧郁 2024-08-23 04:10:43

您应该查看 WCF REST 入门工具包,并观看 HTTP 纯 XML (POX) 服务 逐步解释了如何做到这一点 - 创建一个将接受和处理纯 XML 流的 WCF REST 服务。

强烈推荐 Pluralsight 的所有 WCF 和 WCF REST 截屏视频!这是关于如何开始和使用 WCF 的优秀材料。

除此之外,如果您有任何问题或了解更多信息,MSDN WCF 开发人员中心是您的第一联系点关于 WCF 和 WCF REST。

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.

忆离笙 2024-08-23 04:10:43

我不知道为什么你从这里删除了正确答案,但昨天我在这里得到了正确答案。它是:-(谁能告诉我如何使用 HTTPS 协议做同样的事情?)

    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();               
                 }
    } 

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();               
                 }
    } 
ゞ花落谁相伴 2024-08-23 04:10:43

是的,您可以使用 HTTPS 协议做同样的事情。您必须在请求之前添加此代码:

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

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

             return validationResult;
         };

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;
         };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文