我正在使用 monotouch 为 iOS 6+ 开发我的 iOS 应用程序。应用程序的基础是从用户介绍的服务器下载一些数据。
该服务器可以使用 http 或 https。我使用以下代码进行下载:
System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create (Url);
_HttpWebRequest.AllowWriteStreamBuffering = true;
_HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
_HttpWebRequest.Timeout = 15000;
_HttpWebRequest .Method ="GET";
System.Net.WebResponse _WebResponse = null;
_WebResponse = _HttpWebRequest.GetResponse ();
System.IO.Stream _WebStream = _WebResponse.GetResponseStream ();
var bytes = HelperMethods .ReadToEnd (_WebStream);
_WebResponse.Close ();
_WebResponse.Close ();
return System.Text.Encoding.UTF8.GetString(bytes);
它在服务器运行时有效,但当服务器运行时,http
我https
应该https://
在主机名之前添加才能正常工作。那么如何在发送请求之前检测主机是否使用 https 或 http。