小妹求救https访问返回结果不变的问题
环境IIS7 证书利用IIS设置里的创建自签名证书。
主要问题,我设立了一个网站用https方式访问。
获取两个参数,返回页面一个OK和Error的值。
WinForm里访问这个网站,但是每次访问得到的结果都是一个,
我用GET和POST方法均返回“OK”,无论怎么修改传递的值都一样,请帮我看看哪里出问题了!
Default.aspx 这个是Https访问的地址
namespace SSLWeb{ public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string user_name = Request.QueryString["user_name"]; string password = Request.QueryString["password"]; StreamWriter writer = new StreamWriter("D:\\test.txt"); writer.WriteLine("user_name:" + user_name); writer.WriteLine("password:" + password); writer.WriteLine("Time:" + System.DateTime.Now); if (user_name == "admin" && password == "admin") { writer.WriteLine("OK"); Response.Write("OK"); } else { writer.WriteLine("Error"); Response.Write("Error"); } writer.WriteLine("--------------------------------"); writer.Close(); } } }}
namespace SSLWindows{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { #region Get方法 string userName = "1"; string password = "1"; string tagUrl = "https://192.168.0.62/Default.aspx?user_name="+userName+"&password="+password; CookieCollection cookies = new CookieCollection(); HttpWebResponse response = HttpWebResponseUtility.CreateGetHttpResponse(tagUrl, null, null, cookies); string cookieString = response.StatusDescription; if (cookieString == null) { MessageBox.Show("null"); } else { MessageBox.Show(cookieString); } #endregion #region Post方法 //string loginUrl = "https://192.168.0.62/Default.aspx"; //string userName = "1"; //string password = "1"; //Encoding encoding = Encoding.GetEncoding("gb2312"); //IDictionary<string, string> parameters = new Dictionary<string, string>(); //parameters.Add("@user_name", userName); //parameters.Add("@password", password); //HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, // parameters, null,null, encoding,null); ////StreamReader re = new StreamReader(request.GetResponse().GetResponseStream()); //string cookieString = response.StatusDescription; //if (cookieString == null) //{ // MessageBox.Show("null"); //} //else //{ // MessageBox.Show(cookieString); //} #endregion } catch (Exception ex) { MessageBox.Show(ex.Message); } } }}
namespace SSLWindows{ public class HttpWebResponseUtility { private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; " +".NET CLR 1.1.4322; .NET CLR 2.0.50727)"; /// <summary> /// 创建GET方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } HttpWebRequest request = null; //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); //request = WebRequest.Create(url) as HttpWebRequest; } else { request =(HttpWebRequest) WebRequest.CreateDefault(new Uri(url)); } request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = DefaultUserAgent; if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return response; } /// <summary> /// 创建POST方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="parameters">随同请求POST的参数名称及参数值字典</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="requestEncoding">发送HTTP请求时所用的编码</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreatePostHttpResponse(string url,IDictionary<string,string> parameters, int? timeout, string userAgent,Encoding requestEncoding,CookieCollection cookies) { //挂接验证服务端证书的回调 ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback; if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } if(requestEncoding==null) { throw new ArgumentNullException("requestEncoding"); } HttpWebRequest request=null; //如果是发送HTTPS请求 if(url.StartsWith("https",StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = (HttpWebRequest)WebRequest.Create(url); //request.ProtocolVersion = HttpVersion.Version10; //request = WebRequest.Create(url) as HttpWebRequest; //request.ProtocolVersion=HttpVersion.Version10; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } else { request.UserAgent = DefaultUserAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } //如果需要POST数据 if(!(parameters==null||parameters.Count==0)) { StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, parameters[key]); } else { buffer.AppendFormat("{0}={1}", key, parameters[key]); } i++; } byte[] data = requestEncoding.GetBytes(buffer.ToString()); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return response; } private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; //总是接受 } }}
string cookieString = response.StatusDescription;