c# - 从 C# 在 json 中通过 SSL 向 REST 发送发布请求

标签 c# json ssl

我在谷歌上查了每一页都没有明确的答案 现在这是我的代码:

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.JSONRestSharp

关于c# - 从 C# 在 json 中通过 SSL 向 REST 发送发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40738568/

相关文章:

c# - 在aspx.cs中创建onclick函数的c#代码

jquery - 将json加载到变量中

ssl - 在 Tomcat 中安装证书链 - Jelastic

c# - .Net Core 使用 IEnumerables<T> 序列化对象以另存为 XML

c# - 重写 lambda 扩展方法

c# - 读取包含希腊字符的 CSV 文件

java - 使用 HttpClient 4 通过 WiFi 进行 HTTPS 身份验证

json - 与经典规范化表相比,postgres JSON 索引是否足够高效?

javascript - 如何在 localStorage API HTML5 中保存函数

node.js - 在 Heroku 上强制 Node.js 应用使用 HTTPS