//---- 钉钉相关 开始 ------//
public class HelperTools
{
string DingAppkey = "dingAAAAAAr4jrr";//钉钉 应用的唯一标识key
string DingAppsecret = "Rdh-1XL5MmTsBBBBBBhY9EHqEgwkVi_kOQzORtIO7BsHiXLZ4yP4fjh1CzR";//钉钉 应用的密钥
public static string dingAccessToken = ""; //钉钉接口调用凭证
// <summary>
/// 调用钉钉的api需要一个AccessToken,我们先获取这个AccessToken。
/// </summary>
/// <returns></returns>
public string GetDingAccessToken()
{
string access_token = string.Empty;
try
{
string accessToken = string.Empty;
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.);
OapiGettokenRequest request = new OapiGettokenRequest();
request.Appkey = DingAppkey;//钉钉 应用的唯一标识key
request.Appsecret = DingAppsecret;//钉钉 应用的密钥
request.SetHttpMethod("GET");
OapiGettokenResponse response = client.Execute(request);
access_token = response.AccessToken;
}
catch (Exception ex)
{
}
return access_token;
}
/// <summary>
/// 指定Post地址使用Get 方式获取全部字符串
/// </summary>
/// <param name="url">请求后台地址</param>
/// <returns></returns>
public string Post(string url, Dictionary<string, string> dic)
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
#region 添加Post 参数
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
#endregion
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}
/// <summary>
/// 发送Get请求
/// </summary>
/// <param name="url">地址</param>
/// <param name="dic">请求参数定义</param>
/// <returns></returns>
public string Get(string url, Dictionary<string, string> dic)
{
string result = "";
StringBuilder builder = new StringBuilder();
builder.Append(url);
if (dic.Count > 0)
{
builder.Append("?");
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
//添加参数
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
try
{
//获取内容
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
finally
{
stream.Close();
}
return result;
}
}
//------ 钉钉相关 结束 ------//