代码人生的小狗窝

一行行枯燥的代码,却描绘出人生的点点滴滴

您现在的位置是:首页>_C#

用winform做的HttpWebRequest 模拟登陆器,post参数要怎么暗文发送

发布时间:2018-07-22浏览(2487)

    用winform做的HttpWebRequest 模拟登陆器,post参数要如何暗文发送?
    用了Encrypt()不是会改变原值吗?发送的账号密码就会提示不正确,怎么加密呢?




                Uri uri = new Uri(web_url);
                NameValueCollection values = new NameValueCollection();
                values.Add("username", this.Encrypt(username, key));
                values.Add("password", this.Encrypt(password, key));
                string data = this.PostData(uri, values, true);



    private string PostData(Uri uri, NameValueCollection values, [Optional, DefaultParameterValue(true)] bool status)
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.Accept = "*/*";
                    request.Headers.Add("Accept-Encoding: gzip,deflate");
                    request.Headers.Add("Accept-Language: zh-cn");
                    request.ServicePoint.Expect100Continue = false;
                    request.KeepAlive = true;
                    request.UserAgent = "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25";
                    if (status)
                    {
                        CookieContainer container = new CookieContainer();
                        container.Add(uri, new Cookie("cookie", this.SessionID));
                        //container.Add(uri, new Cookie("Version", this.Version));
                        request.CookieContainer = container;
                    }
                    string s = string.Empty;
                    if (values != null)
                    {
                        foreach (string str2 in values.Keys)
                        {
                            s = s + string.Format("{0}={1}&", str2, values[str2]);
                        }
                        s = s.Remove(s.Length - 1);
                    }
                    byte[] bytes = Encoding.UTF8.GetBytes(s);
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                    StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8);
                    return reader.ReadToEnd();