这是我现在的C#代码。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.upcloud.com/1.2/account");
string authInfo = "username:password";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers.Add("Authorization", "Basic " + authInfo);
request.ContentType = "application/json";
request.Method = WebRequestMethods.Http.Get;
request.AllowAutoRedirect = true;
request.Proxy = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader streamreader = new StreamReader(stream);
string s = streamreader.ReadToEnd();
Console.Write(s);
代码对我来说看起来不错,但是在尝试获取响应( HttpWebResponse response =(HttpWebResponse)request.GetResponse(); )时,出现了此异常System.Net.WebException: The underlying connection was closed
。
我想要做的基本上是将https://www.upcloud.com/support/getting-started-upcloud-api/这里报告的python示例“翻译”为C#,但是无论我做了多少尝试,我仍然没有找到解决方案。预先感谢。