简体   繁体   English

NetFramework转换为Net Core(HttpWebRequest)

[英]NetFramework convert to Net Core (HttpWebRequest)

I have a library for 4.5 net fw. 我有一个4.5净周转的图书馆。 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: 来自VS的错误代码(屏幕)我的代码:

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();
                    }
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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