C# 我尝试发布一个json对象以获取C中的身份验证令牌#

C# 我尝试发布一个json对象以获取C中的身份验证令牌#,c#,json,httpresponse,C#,Json,Httpresponse,我尝试发布一个json对象以获取C#中的身份验证令牌。 我是新手,我试着自己去做,但我遇到了一个例外(底层连接已关闭:连接已关闭) 意外地) 我有一个带文档的API 该代码: var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://URL/api/authenticate/authenticate"); httpWebRequest.ContentType = "application/json"; httpWebReque

我尝试发布一个json对象以获取C#中的身份验证令牌。 我是新手,我试着自己去做,但我遇到了一个例外(底层连接已关闭:连接已关闭) 意外地) 我有一个带文档的API

该代码:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://URL/api/authenticate/authenticate");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = "{\"applicatoinKey\":\"MYAPPLICATIONKEY\"," +
                    "\"userSecret\":\"MYUSERSECRET\"}";

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}

我不知道我在哪里犯了错误。

您不需要
streamWriter.Close()使用
语句时使用
时。在代码的哪一点出现异常?@danielshillock ok但仍然相同error@Smartis这里:var httpResponse=(HttpWebResponse)httpWebRequest.GetResponse();在调用GetResponse()方法之前,请尝试添加“System.Net.ServicePointManager.SecurityProtocol=System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Ssl3;”:不需要
streamWriter.Close()使用
语句时使用
时。在代码的哪一点出现异常?@danielshillock ok但仍然相同error@Smartis这里:var httpResponse=(HttpWebResponse)httpWebRequest.GetResponse();在调用GetResponse()方法之前,请尝试添加“System.Net.ServicePointManager.SecurityProtocol=System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Ssl3;”: