我在谷歌上查了每一页都没有明确的答案 现在这是我的代码:
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.hesabit.com/oauth2/token");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
string result_st;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"code\":\"{atho code}\"," +
"\"grant_type\":\"authorization_code\"," +
"\"client_id\":\"{client_id}\"," +
"\"client_secret\":\"{client_secret}\"," +
"\"redirect_uri\":\"{redirect_uri}\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
result_st = result;
}
return result_st;
}
catch (Exception ex)
{
return ex.ToString();
}
最佳答案
你的代码看起来很原始,你应该使用一些有用的库,比如 Newtonsoft.JSON和 RestSharp
关于c# - 从 C# 在 json 中通过 SSL 向 REST 发送发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40738568/