GET请求:
/// <summary> /// 获取accessToken /// </summary> /// <param name="corpid">ID</param> /// <param name="corpsecret">密钥</param> /// <returns></returns> static string GetQYAccessToken(string corpid, string corpsecret) { string getAccessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}"; string accessToken = ""; string respText = ""; //获取josn数据 string url = string.Format(getAccessTokenUrl, corpid, corpsecret); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream resStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(resStream, Encoding.Default); respText = reader.ReadToEnd(); resStream.Close(); } try { JavaScriptSerializer Jss = new JavaScriptSerializer(); Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText); //通过键access_token获取值 accessToken = respDic["access_token"].ToString(); } catch (Exception ex) { } return accessToken; }
带参数POST请求:
/// <summary> /// Post数据接口 /// </summary> /// <param name="postUrl">接口地址</param> /// <param name="paramData">提交json数据</param> /// <param name="dataEncode">编码方式</param> /// <returns></returns> static string PostWebRequest(string postUrl, string paramData, Encoding dataEncode) { string ret = string.Empty; try { byte[] byteArray = dataEncode.GetBytes(paramData); //转化 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { return ex.Message; } return ret; }