微信C#响应代码

暂时只把代码贴上来,自己Mark着

 

1. 从微信GET access_token

string urlGet = @"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3de0ac694cbc35f1&secret=ab49137d7f8098ec655b5614c11373cb";

WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); 
byte[] responseData = webClient.DownloadData(urlGet); 
string srcString = Encoding.UTF8.GetString(responseData);//解码  

wx w = new wx();
w = (wx)  JsonConvert.DeserializeObject(json , typeof(wx));

Console.WriteLine(srcString);
Console.WriteLine(w.access_token);
Console.Read();

/*
    class wx
    {
        public string access_token;
        public int expires_in;
    }

*/

 

 

 

2. 向微信POST消息

/*



class WXPOST
{
    public string touser;
    public string msgtype;
    public TTT text = new TTT() ;
}



class TTT
{
    public string content;
}


*/

WXPOST wxp = new WXPOST(); wxp.touser = "o99EfuLJKA39QTQglvjMEVZBFT6k"; wxp.msgtype = "text"; wxp.text.content = "hello world"; string postString = JsonConvert.SerializeObject(wxp); Console.WriteLine(postString); byte[] postData = Encoding.UTF8.GetBytes(postString); string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=XXJ40LY01x3CE1zjT3jhlB6LYJXbhbPdMV9y-2fvrPADtxQq7JiMk8b9ypA_BenE4lGr0oM4k462cGvkH3eEoIB0xWMWsTBFzKCT6-lO7-T3I6qZUBpbJqw1cEdgbwys5pdgaqFRTJ1Y6QrEHPA5dA";//地址 WebClient webClient = new WebClient(); webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可 byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流 string srcString = Encoding.UTF8.GetString(responseData);//解码 Console.WriteLine(srcString); Console.Read();

posted on 2014-01-05 23:42  EasyBI  阅读(450)  评论(0)    收藏  举报

导航