• 金数据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
    }

  • 相关阅读:
    oracle 查询 当前最大时间的value的值
    Visual Studio《加载此属性页时出错》的解决办法
    (转) 关于在IE6下 无法跳转问题
    LINQ TO XML 个人的一些心得1
    CSS实现单行、多行文本溢出显示省略号(…)
    html 图像映射(一个图像多个连接)
    JS页面跳转大全
    首行负缩进达到内容对齐的目的
    HTML图片死活不显示
    JS高级. 06 缓存、分析解决递归斐波那契数列、jQuery缓存、沙箱、函数的四种调用方式、call和apply修改函数调用方法
  • 原文地址:https://www.cnblogs.com/Andy-Blog/p/6401703.html
Copyright © 2020-2023  润新知