C#模拟HTTP POST 请求
标签:ons 写入 javascrip ram 请求 ade 编码 using access
GET请求:
///
/// 获取accessToken
///
/// ID
/// 密钥
///
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();
Dictionarystring, object> respDic = (Dictionarystring, object>)Jss.DeserializeObject(respText);
//通过键access_token获取值
accessToken = respDic["access_token"].ToString();
}
catch (Exception ex) { }
return accessToken;
}
带参数POST请求:
///
/// Post数据接口
///
/// 接口地址
/// 提交json数据
/// 编码方式
///
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;
}
C#模拟HTTP POST 请求
标签:ons 写入 javascrip ram 请求 ade 编码 using access
原文地址:https://www.cnblogs.com/25miao/p/9382370.html
评论