你的位置:首页 > ASP.net教程

[ASP.net教程]C#调用金数据API


首先,吐槽一下金数据的API文档 http://help.jinshuju.net/articles/api-intro.html 写的很粗糙啊...反正我是没太看明白

拿表单api举例,只告诉你了个地址https://jinshuju.net/api/v1/forms/ex27t2,然后呢,然后没有了,估计大家也能猜到ex27t2应该是变化的,但是填什么呢...

 

这里应该填你表单的一个值,好像只有在这种地方有这个值,其他地方我没发现..

而且提交咨询工单也没用,给我回的邮件仅回复了“我们目前不提供这类技术支持。若有其他问题请继续提交工单。如果您还有问题,可以查看我们的帮助中心。欢迎关注金数据官方微信(金数据服务号/jinshuju-service)。”

自己动手,丰衣足食吧...

好了,不多说其他的了,直接上代码

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  }

最后,还得吐槽下金数据数据API,明明说好了,要是如果不满足校验条件(必填/重复等),金数据会返回400,并给出提示。

但是!并没有提示,只是会返回400 Bad Request!害得我搞了半天才明白,以为是页面请求不存在了或者其他请求错误了,因为说好的提示并没有给出来..