zoukankan      html  css  js  c++  java
  • 金数据api

    原文:http://www.cnblogs.com/mvv118/p/5213660.html

    此贴备用

    public class JinShuJu
    {
    private static string Key = "9hh6fIbLOqDrgin4Q_r6X";
    private static string Secret = "z3RSib8HeQBfau6zwRpRo";

    /// <summary>
    /// 获取表单API
    /// </summary>
    /// <returns></returns>
    public static string GetFromAPI()
    {
    string uri = "https://jinshuju.net/api/v1/forms/RsE5EC";
    string result = string.Empty;
    WebRequest request = WebRequest.Create(new Uri(uri));

    request.Credentials = GetCredentialCache(uri, Key, Secret);
    request.Headers.Add("Authorization", GetAuthorization(Key, Secret));

    request.Timeout = 2000;
    WebResponse response = request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader sr = new StreamReader(stream);
    result = sr.ReadToEnd();
    sr.Close();
    stream.Close();

    return result;
    }

    /// <summary>
    /// 提交数据API
    /// </summary>
    /// <param name="JSONData"></param>
    /// <returns></returns>
    public static string PostFromAPI(string JSONData)
    {
    string uri = "https://jinshuju.net/api/v1/forms/RsE5EC";
    string result = string.Empty;
    WebRequest request = WebRequest.Create(new Uri(uri));

    request.Credentials = GetCredentialCache(uri, Key, Secret);
    request.Headers.Add("Authorization", GetAuthorization(Key, Secret));

    request.Timeout = 2000;

    byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
    request.Method = "POST";
    request.ContentType = "application/json";
    string paraUrlCoded = JSONData;
    byte[] payload;
    payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
    request.ContentLength = payload.Length;
    Stream writer = request.GetRequestStream();
    writer.Write(payload, 0, payload.Length);
    writer.Close();

    WebResponse response = request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader sr = new StreamReader(stream);
    result = sr.ReadToEnd();
    sr.Close();
    stream.Close();

    return result;
    }

    #region # 生成 Http Basic 访问凭证 #
    private static CredentialCache GetCredentialCache(string uri, string username, string password)
    {
    string authorization = string.Format("{0}:{1}", username, password);
    CredentialCache credCache = new CredentialCache();
    credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
    return credCache;
    }
    private static string GetAuthorization(string username, string password)
    {
    string authorization = string.Format("{0}:{1}", username, password);
    return "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization));
    }
    #endregion
    }

  • 相关阅读:
    10 个最佳的网站分析方法
    网站优化:测试网站速度的8款免费工具推荐
    8 个最棒的 .NET 开发相关工具
    10 个文件和文档的比较工具
    asp.net中调用javascript自定义函数的方法(包括引入JavaScript文件)总结
    如何在ashx页面获取Session值
    java-信息安全(十一)-非对称加密算法ECC
    java-信息安全(十)-数字签名算法DSA
    java-信息安全(九)-基于DH,非对称加密,对称加密等理解HTTPS
    java-信息安全(八)-迪菲-赫尔曼(DH)密钥交换
  • 原文地址:https://www.cnblogs.com/Andy-Blog/p/6401703.html
Copyright © 2011-2022 走看看