简体   繁体   中英

NetFramework convert to Net Core (HttpWebRequest)

I have a library for 4.5 net fw. I need do the same but for net core.

Big beer for person which can repair this..

Code from VS with errors (screen) MY code:

string returnData = String.Empty;

            var webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
            if (webRequest != null)
            {
                webRequest.Accept = "*/*";
                webRequest.UserAgent = ".NET";
                webRequest.Method = method;
                webRequest.ContentType = "application/json";
                webRequest.Host = "coinbase.com";

                string nonce = Convert.ToInt64(DateTime.Now.Ticks).ToString();
                string message = nonce + url;
                string signature = HashEncode(HashHMAC(StringEncode(API_SECRET), StringEncode(message)));

                var whc = new WebHeaderCollection();
                whc.Add("ACCESS_KEY: " + API_KEY);
                whc.Add("ACCESS_SIGNATURE: " + signature);
                whc.Add("ACCESS_NONCE: " + nonce);
                webRequest.Headers = whc;

                using (WebResponse response = webRequest.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(stream);
                        returnData = reader.ReadToEnd();
                    }
                }
            }

You can use my code as below. Hope to help, my friend:

var webRequest = WebRequest.Create(url) as HttpWebRequest;
            if (webRequest != null)
            {
                webRequest.Accept = "*/*";
                webRequest.UserAgent = ".NET";
                webRequest.Method = WebRequestMethods.Http.Post;
                webRequest.ContentType = "application/json";
                webRequest.Host = "coinbase.com";

                var whc = new WebHeaderCollection
                {
                    "ACCESS_KEY: " + API_KEY,
                    "ACCESS_SIGNATURE: " + signature,
                    "ACCESS_NONCE: " + nonce
                };
                webRequest.Headers = whc;

                using (WebResponse response = webRequest.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(stream);
                        returnData = reader.ReadToEnd();
                    }
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM