|
本帖最后由 yodong 于 2015-5-15 16:46 编辑
下面代码是C#发送微信企业号的消息的代码.那位朋友能不能把它改为VBA代码?
获取微信企业号令牌:
private void button1_Click(object sender, EventArgs e)
{
string tt=GetAaccessToken();
int t =tt.Length;
textBox3.Text = tt.Substring(17, t - 37);// GetAaccessToken();
}
private string GetAaccessToken()
{
CookieContainer cookieContainer = new CookieContainer();
string formUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + textBox1.Text + "&corpsecret=" + textBox2.Text;
string formData = "";
// 将提交的字符串数据转换成字节数组
byte[] postData = Encoding.UTF8.GetBytes(formData);
// 设置提交的相关参数
HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
Encoding myEncoding = Encoding.GetEncoding("gb2312");
request.Method = "POST";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.CookieContainer = cookieContainer;
request.ContentLength = postData.Length;
// 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
HttpWebResponse response;
Stream responseStream;
StreamReader reader;
string srcString;
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
srcString = reader.ReadToEnd();
reader.Close();
return srcString;
}
发送信息:
private void button2_Click(object sender, EventArgs e)
{
string url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + textBox3.Text;
Send(url, textBox5.Text);
MessageBox.Show("发送成功!");
}
private void Send(string url, string body)
{
CookieContainer cookieContainer = new CookieContainer();
string formUrl = url;
string formData = body;
// 将提交的字符串数据转换成字节数组
byte[] postData = Encoding.UTF8.GetBytes(formData);
// 设置提交的相关参数
HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
Encoding myEncoding = Encoding.GetEncoding("gb2312");
request.Method = "POST";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.CookieContainer = cookieContainer;
request.ContentLength = postData.Length;
// 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
HttpWebResponse response;
Stream responseStream;
StreamReader reader;
string srcString;
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
srcString = reader.ReadToEnd();
reader.Close();
}
|
|