Office中国论坛/Access中国论坛
标题:
哪位大神,把下面C#的代码改为VBA.
[打印本页]
作者:
yodong
时间:
2015-5-15 16:45
标题:
哪位大神,把下面C#的代码改为VBA.
本帖最后由 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();
}
作者:
roych
时间:
2015-5-15 17:29
不是直接改就可以的
1、很多引入库VB里不一定有,例如:
System.IO
2、解码函数写法也不一样,例如:
byte[] postData = Encoding.UTF8.GetBytes(formData)
没弄清是16进制编码还是8进制编码之前,没办法调用解码函数的。虽然大部分php自带的编码函数都是16进制。
3、另外,发送http请求时可能涉及到加密方法。这也是一个难题。
最后,建议楼主别折腾这个了。之前闲着无聊时,看过一些关于公众号开发的程序,基本都是PHP接口多。如果真要处理的话,不放先从PHP开始吧。虽说PHP也不算太难,不过牵涉到服务器响应部分,还是挺麻烦的。
作者:
todaynew
时间:
2015-5-15 18:01
该段代码中许多类,在vba中都不存在。所以不大可能简单的转换过来。
作者:
tmtony
时间:
2015-5-15 18:24
开发过公众号的微信程序。必须要有网站服务器 做一个中转网页才可以,不能只靠客户端
因为微信后台要设置一个转向出口。
但企业号 可能有些不同
作者:
风中漫步
时间:
2015-5-15 18:39
本帖最后由 风中漫步 于 2015-5-15 18:44 编辑
楼主做到哪步了?{:soso_e103:}
作者:
zpy2
时间:
2015-5-16 05:16
用xmlhttp就行了 转码用 recordset的stream
作者:
ak47ok
时间:
2015-11-28 13:02
每次回帖、谢谢!辛苦了。
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3