Esempio n. 1
0
        /// <summary>
        /// Get the signature based on the timestamp.  If the timestamp is null, create
        /// a new timestamp.
        /// </summary>
        /// <param name="timeStamp">
        /// The time stamp.
        /// </param>
        /// <returns>
        /// The calculate signature <see cref="string"/>.
        /// </returns>
        public string GetWallmartSignature(string timeStamp)
        {
            TimeStamp = timeStamp ?? GetTimestampInJavaMillis();

            // Append values into string for signing
            var message = ConsumerId + "\n" + RequestUrl + "\n" + RequestMethod.ToUpper() + "\n" + TimeStamp + "\n";

            RsaKeyParameters rsaKeyParameter;

            try
            {
                var keyBytes = Convert.FromBase64String(this.PrivateKey);
                var asymmetricKeyParameter = PrivateKeyFactory.CreateKey(keyBytes);
                rsaKeyParameter = (RsaKeyParameters)asymmetricKeyParameter;
            }
            catch (Exception)
            {
                throw new Exception("Unable to load private key");
            }

            var signer = SignerUtilities.GetSigner("SHA256withRSA");

            signer.Init(true, rsaKeyParameter);
            var messageBytes = Encoding.UTF8.GetBytes(message);

            signer.BlockUpdate(messageBytes, 0, messageBytes.Length);
            var signed = signer.GenerateSignature();
            var hashed = Convert.ToBase64String(signed);

            return(hashed);
        }
        protected string GetBaseString()
        {
            string requestMethod = RequestMethod.ToUpper();
            string normalized    = GetNormalizedRequestParameters();
            string absoluteUrl   = HttpUtil.RemoveQueryString(Url);

            string baseString = string.Format("{0}&{1}&{2}",
                                              requestMethod, HttpUtil.Esc(absoluteUrl), HttpUtil.Esc(normalized));

            return(baseString);
        }
Esempio n. 3
0
        public string HttpRequest(IDictionary <string, string> dicParams, string responseEncoding = "UTF-8")
        {
            var           responseStr = "";
            HttpWebHelper http        = new HttpWebHelper();

            if (RequestMethod.ToUpper() == "GET")
            {
                var _payUrl = HttpWebHelper.CreateParameter(dicParams);
                responseStr = http.Get(_payUrl, Encoding.GetEncoding(responseEncoding));
            }
            else
            {
                responseStr = http.Post(PayUrl, dicParams, Encoding.GetEncoding(responseEncoding), Encoding.GetEncoding(responseEncoding));
            }
            return(responseStr);
        }
Esempio n. 4
0
        public string HttpRequest(string url, string param)
        {
            if (string.IsNullOrEmpty(url) || (!url.Contains("http://") && !url.Contains("https://")))
            {
                return("");
            }
            int retry = 0;

            while (true)
            {
                string paraminfo  = "";
                string strGethtml = "";
                try
                {
                    if (!string.IsNullOrEmpty(param))
                    {
                        if (RequestMethod.ToUpper() == "POST")
                        {
                            paraminfo = param;
                        }
                        else
                        {
                            url = url + "?" + param;
                        }
                    }

                    HttpWebRequest mywr = (HttpWebRequest)WebRequest.Create(url);
                    mywr.Proxy             = RequesProxy;
                    mywr.Method            = RequestMethod;
                    mywr.Accept            = RequestAccept;
                    mywr.ContentType       = RequestContentType;
                    mywr.AllowAutoRedirect = RequestAutoRedirect;
                    if (!string.IsNullOrEmpty(RequestReferer))
                    {
                        mywr.Referer = RequestReferer;
                    }
                    mywr.UserAgent = RanAgent
                        ? UserAgentList[Rang.Next(0, UserAgentList.Length)]
                        : RequestUserAgent;
                    if (Headers != null && Headers.Count > 0)
                    {
                        foreach (KeyValuePair <string, string> item in Headers)
                        {
                            mywr.Headers.Add(item.Key, item.Value);
                        }
                    }
                    mywr.KeepAlive = RequestKeepAlive;
                    mywr.Timeout   = RequestTimeout;
                    if (RequestCookies.Count > 0)
                    {
                        mywr.CookieContainer = RequestCookies;
                    }


                    //把参数用流对象写入request对象中
                    if (paraminfo != "")
                    {
                        byte[] postbyte = Encoding.ASCII.GetBytes(paraminfo);
                        mywr.ContentLength = postbyte.Length;
                        Stream newStream = mywr.GetRequestStream();
                        newStream.Write(postbyte, 0, postbyte.Length);
                        newStream.Close();
                    }

                    HttpWebResponse mywrp = (HttpWebResponse)mywr.GetResponse();
                    //mywrp.Headers.ToString();
                    if (mywrp.ResponseUri.AbsoluteUri != url)
                    {
                        LogServer.WriteLog("url old:" + url + "new " + mywrp.ResponseUri, "UrlChange");
                        //return "";
                    }
                    if (ResultResponseHeader == null)
                    {
                        ResultResponseHeader = new Dictionary <string, string>();
                    }
                    else if (ResultResponseHeader.Count > 0)
                    {
                        ResultResponseHeader.Clear();
                    }

                    for (int i = 0; i < mywrp.Headers.Count; i++)
                    {
                        if (headKey.Contains(mywrp.Headers.Keys[i]))
                        {
                            continue;
                        }

                        ResultResponseHeader.Add(mywrp.Headers.Keys[i], mywrp.Headers.Get(i));
                    }

                    //SimulationCookie.SetCookie(mywr.Host, mywrp.Cookies);
                    Stream responseStream = mywrp.GetResponseStream();
                    if (mywrp.ContentEncoding.ToLower().Contains("gzip"))
                    {
                        if (responseStream != null)
                        {
                            responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                        }
                    }

                    int scode = (Int32)mywrp.StatusCode;
                    if (scode != 200)
                    {
                        return("Error StatusCode:" + scode);
                    }

                    Encoding encodingType;
                    string   tempType = mywrp.Headers["Content-Type"] ?? "";
                    tempType = tempType.ToLower();
                    if (tempType.Contains("gbk") || tempType.Contains("gb2312") || tempType.Contains("utf-8"))
                    {
                        if (tempType.Contains("gbk") || tempType.Contains("gb2312"))
                        {
                            encodingType = Encoding.GetEncoding("GBK");
                        }
                        else
                        {
                            encodingType = Encoding.UTF8;
                        }

                        if (responseStream == null)
                        {
                            return(strGethtml);
                        }
                        StreamReader sr = new StreamReader(responseStream, encodingType);
                        strGethtml = sr.ReadToEnd().Trim();
                        mywrp.Close();
                        sr.Close();
                        return(strGethtml);
                    }


                    if (mywrp.StatusCode == HttpStatusCode.Redirect)
                    {
                        strGethtml += string.Format("ResponseUri:{0}", mywrp.ResponseUri);
                    }

                    mywrp.Close();
                    return(strGethtml);

                    //if (responseStream == null) return strGethtml;
                    //StreamReader sr = new StreamReader(responseStream, RequestEncoding);
                    //strGethtml = sr.ReadToEnd().Trim();
                    //mywrp.Close();
                    //sr.Close();
                }
                catch (Exception ex)
                {
                    if (retry < 3)
                    {
                        retry++;

                        Thread.Sleep(1000);
                        continue;
                    }

                    if (ex is WebException)
                    {
                        var webEx = ex as WebException;
                        if (RequesProxy != null)
                        {
                            LogServer.WriteLog("url:" + url + "Proxy:" + RequesProxy.Address + "\t" + webEx.Message, "HtmlAnalysis");
                        }
                        else
                        {
                            LogServer.WriteLog("url:" + url + "" + webEx.Message, "HtmlAnalysis");
                        }
                        return("");
                    }

                    LogServer.WriteLog("get url:" + url + ex, "HtmlAnalysis");
                    return("");
                }
            }
        }