2009-12-29 209 views
0

我有下面的代碼正在發送HTTP協議的XML文件,並獲得響應作爲xml文件從網絡服務器和它的HTTP協議工作正常,但現在我需要發送這樣的XML文件轉換爲HTTPS協議(而不是http),並且需要以xml文件的形式獲取響應。發送XML文件,並從HTTP響應代碼:將HTTP請求代碼轉換爲HTTPS請求代碼

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


     } 
    } 

回答

1

不應該存在於應用程序級別在你的代碼太大的區別,如HTTP或HTTPS的區別僅在傳輸層,沒有。 這裏可能會出現的問題是,如果targetUri中使用的服務器證書在您的服務器上受信任。在這種情況下,HTTPS身份無法驗證。

+0

我對你的理解是:你在說如果我們想用HTTPS而不是HTTP工作,那麼只有上面代碼中的URL纔會變成HTTPS而不是HTTP。否則他們不需要在代碼中進行更改? – 2009-12-29 07:46:19

+0

是的,只要您信任來自服務器的HTTPS證書即可。 – naivists 2009-12-29 09:08:35