示例#1
0
        private static void RefresTokens(string cookie)
        {
            if (string.IsNullOrEmpty(cookie))
            {
                return;
            }

            var canaryIndex  = cookie.IndexOf("X-OWA-CANARY=");
            var backEndIndex = cookie.IndexOf("X-BackEndCookie=");

            var backEndCookie = cookie.Substring(backEndIndex, 134 + 16);
            var token         = cookie.Substring(canaryIndex, 76 + 13);

            if (!string.IsNullOrEmpty(CurrentCookie))
            {
                var oldCanaryIndex  = CurrentCookie.IndexOf("X-OWA-CANARY=");
                var oldBackEndIndex = CurrentCookie.IndexOf("X-BackEndCookie=");

                var oldBackEndCookie = CurrentCookie.Substring(oldBackEndIndex, 134 + 16);
                var oldToken         = CurrentCookie.Substring(oldCanaryIndex, 76 + 13);

                CurrentToken  = new string(token.Skip(13).ToArray());
                CurrentCookie = CurrentCookie.Replace(oldBackEndCookie, backEndCookie).Replace(oldToken, token);
            }
        }
示例#2
0
        /// <summary>
        /// HTTP(GET/POST 键值对参数列表)
        /// </summary>
        /// <returns>响应内容</returns>
        public string SendPost()
        {
            if (this.files.Count > 0)
            {
                return(HttpUploadFile(this.files.ToDictionary(k => k.Key, v => v.Value as object)));
            }

            HttpWebRequest  req       = null;
            HttpWebResponse rsp       = null;
            Stream          reqStream = null;
            Encoding        encoding  = null;

            try
            {
                if (method.ToLower() == "post")
                {
                    req                 = (HttpWebRequest)WebRequest.Create(url);
                    req.Method          = "POST";
                    req.KeepAlive       = false;
                    req.ProtocolVersion = HttpVersion.Version10;
                    req.Timeout         = Timeout;
                    //头文件
                    if (headers.Count > 0)
                    {
                        foreach (KeyValuePair <string, string> header in headers)
                        {
                            if (!WebHeaderCollection.IsRestricted(header.Key))
                            {
                                req.Headers.Add(header.Key, header.Value);
                            }
                            else
                            {
                                req.Host        = "14.17.22.55:8028";
                                req.ContentType = "application/json";
                            }
                        }
                    }
                    //证书
                    if (credentials != null)
                    {
                        req.Credentials = credentials;
                    }

                    req.CookieContainer = CurrentCookie ?? (CurrentCookie = new CookieContainer());
                    reqStream           = req.GetRequestStream();
                    if (parameters.Count > 0)
                    {
                        byte[] postData = null;
                        //StreamWriter myStreamWriter = new StreamWriter(reqStream, Encoding.GetEncoding("utf-8"));
                        //postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, false, Encode));
                        postData = Encoding.UTF8.GetBytes(BuildQueryTest(parameters, false, Encode));
                        reqStream.Write(postData, 0, postData.Length);
                    }
                    else
                    {
                        byte[] postData = Encoding.GetEncoding(Encode).GetBytes(url);
                        reqStream.Write(postData, 0, postData.Length);
                    }
                }
                ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return(true); });
                try
                {
                    rsp = (HttpWebResponse)req.GetResponse();
                }
                catch (WebException ex)
                {
                    var rs = ex.Response.GetResponseStream();
                    var sr = new StreamReader(rs, System.Text.Encoding.UTF8);

                    var Error = sr.ReadToEnd();
                    rs.Close();
                    sr.Close();

                    Debug.Write(ex.Message);                                                       //远程服务器返回错误: (400) 错误的请求。
                    var strResponse = GetResponseAsString((HttpWebResponse)ex.Response, encoding); //这样获取web服务器返回数据
                }
                if (CurrentCookie.Count == 0)
                {
                    CurrentCookie.Add(rsp.Cookies);
                }

                encoding = Encoding.GetEncoding(rsp.CharacterSet ?? Encode);
                return(GetResponseAsString(rsp, encoding));
            }
            finally
            {
                if (reqStream != null)
                {
                    reqStream.Close();
                }
                if (rsp != null)
                {
                    rsp.Close();
                }
            }
        }