将HTTP请求码转换为HTTPS请求码

发布于 2024-08-16 09:57:17 字数 1693 浏览 3 评论 0原文

我有以下代码,正在通过 HTTP 协议发送 xml 文件,并从网络服务器以 xml 文件形式获取响应,并且它与 HTTP 协议一起工作正常,但现在我需要将这样的 XML 文件发送到 HTTPS 协议(而不是 http),并且需要从中获取 xml 文件形式的响应。发送 xml 文件并从 HTTP 获取响应的代码是:

    string targetUri = "http://www.hostelspoint.com/xml/xml.php"; /*this will be like: "https://www.hostelspoint.com/xml/xml.php"*/
    System.Xml.XmlDocument reqDoc = new System.Xml.XmlDocument();
    reqDoc.Load(Server.MapPath("~\\getdetail.xml"));
    string formParameterName = "OTA_request";
    string xmlData = reqDoc.InnerXml;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri);
    string sendString = formParameterName + "=" + HttpUtility.UrlEncode(xmlData);
    //string sendString =  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 have got following code witch are sending xml file on HTTP protocol and getting response back as xml file from webserver and its working fine with HTTP protocol, but now i need to send such a XML file to HTTPS protocol (not http) and need to get response as xml file from it. the code to send xml file and get response from HTTP is :

    string targetUri = "http://www.hostelspoint.com/xml/xml.php"; /*this will be like: "https://www.hostelspoint.com/xml/xml.php"*/
    System.Xml.XmlDocument reqDoc = new System.Xml.XmlDocument();
    reqDoc.Load(Server.MapPath("~\\getdetail.xml"));
    string formParameterName = "OTA_request";
    string xmlData = reqDoc.InnerXml;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri);
    string sendString = formParameterName + "=" + HttpUtility.UrlEncode(xmlData);
    //string sendString =  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();


        }
    }  

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

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

发布评论

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

评论(1

一江春梦 2024-08-23 09:57:17

您的代码不应有太大差异,因为 HTTP 或 HTTPS 仅在传输级别有所不同,而不是在应用程序级别有所不同。
这里可能出现的问题是,targetUri 中使用的服务器证书是否在您的服务器上受信任。在这种情况下,无法验证 HTTPS 身份。

There shouldn't be much difference in your code, as HTTP or HTTPS differs only in transport level, not in application level.
What may become a problem here is, if the server certificate used in the targetUri is trusted on your server. In this case the HTTPS identity cannot be verified.

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