|
APICloud 使用C# .net 为后台补充数据ajax post (处理数据库)
[复制链接]
//中间使用的JObject 需要下载Newtonsoft.Json,是一个c#处理json插件。网上自己搜索一下
private void button1_Click(object sender, EventArgs e)
{
string jsonstr = "{'where':{'or':[{'id':'558d1b7c195f1ab51573d054','sex':'true','age':{'lte':44}},{'sex':true}]},'limit':5}";//添加点其他参数实例
jsonstr = "{'where':{'id':'558d1b7c195f1ab51573d054'},'limit':5}";//最简单的查询
JObject sdsad1 = JObject.Parse(jsonstr);
jsonstr = sdsad1.ToString();//主要是里面的'变成",其实可以不用,自己替换也可以,麻烦点而已
string sdfsdf = GetAIPCloudJson(@"https://d.apicloud.com/mcm/api/news?filter=" + jsonstr + "", "get");
//sdfsdf = GetWebContent(@"https://d.apicloud.com/mcm/api/news/558d1b7c195f1ab51573d054", "get");
string jsonstr2 = "";
jsonstr2 = "[" +
"{'title':'测试.net标题yes2','content':'.net内容yes'}," +
"{'title':'测试.net标题yes33','content':'.net内容yes'}"
+ "]";
//jsonstr2 = jsonstr2.Replace("'", "\"");
JArray sdsad = JArray.Parse(jsonstr2);
jsonstr2 = sdsad.ToString();
string sdfsdf2 = PostAIPCloudJson("https://d.apicloud.com/mcm/api/news/", "post", jsonstr2);
}
public string SHA1(string str)
{
SHA1 sha = new SHA1CryptoServiceProvider();
//将mystr转换成byte[]
ASCIIEncoding enc = new ASCIIEncoding();
byte[] dataToHash = enc.GetBytes(str);
//Hash运算
byte[] dataHashed =**.**puteHash(dataToHash);
//将运算结果转换成string
string hash = BitConverter.ToString(dataHashed).Replace("-", "").ToLower();
return hash;
}
string appid = "A6989*******8467"; //应用id
string appkey = "059CDEA5-****-A881-F9AE-9*****37";//应用key
/// <summary>
/// 读写流的超时时限
/// </summary>
public int readWriteTimeout = 15000;
/// <summary>
/// 响应的超时时限
/// </summary>
public int timeout = 30000;
public string GetAIPCloudJson(string strUrl, string strMethod)
{
return PostAIPCloudJson(strUrl, strMethod, "");
}
public string PostAIPCloudJson(string strUrl, string strMethod, string strPostData)
{
DateTime now = DateTime.Now;
string strNow = ((long)(now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds)).ToString();
string appKey22 = SHA1(appid + "UZ" + appkey + "UZ" + strNow) + "." + strNow;
Uri uri = new Uri(strUrl);
string strContent = "";
try
{
HttpWebRequest hw = (HttpWebRequest)WebRequest.Create(uri);
hw.Timeout = timeout;
hw.ReadWriteTimeout = readWriteTimeout;
hw.Method = strMethod;
hw.ContentType = "application/json; charset=utf-8";
hw.Headers.Add("X-APICloud-AppId", appid);
hw.Headers.Add("X-APICloud-AppKey", appKey22);
if (!string.IsNullOrEmpty(strPostData))
{
byte[] data = Encoding.UTF8.GetBytes(strPostData);
hw.ContentLength = data.Length;
Stream stream = hw.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
}
HttpWebResponse res;
try
{
res = (HttpWebResponse)hw.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
Stream mystream = res.GetResponseStream();
StreamReader sr = new StreamReader(mystream, Encoding.GetEncoding("utf-8"));
strContent = sr.ReadToEnd();
sr.Close();
return strContent;
}
catch
{
return "";
}
}
//研究到这里的同学,我们一起研究下,怎样对权限的控制,例如,别人知道你的id和key之后,应该怎样确保安全?//至于角色和用户安全问题,我暂时没找到比较好的教学视频,希望有些同学能提供下相关技术
|
|