欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.net微信服務(wù)號(hào)發(fā)送紅包

 更新時(shí)間:2016年10月18日 16:19:51   作者:天風(fēng)隼  
這篇文章主要為大家詳細(xì)介紹了.net微信服務(wù)號(hào)發(fā)送紅包的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了.net微信紅包發(fā)送代碼,供大家參考,具體內(nèi)容如下

注:需要開(kāi)通微信支付的服務(wù)號(hào)!

//跳轉(zhuǎn)微信登錄頁(yè)面
public ActionResult Index()
{
 ViewBag.url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + {服務(wù)號(hào)appid} + "&redirect_uri=http%3A%2F%2F" + {微信重定向域名(填寫(xiě)程序域名,例如:www.xxxx.com)} + "%2F"+{程序控制器名,例如:Home}+"%2F"+{程序Action名,例如:RedirectWeChat}+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
 return View();
}

//獲取accesstoken(訪(fǎng)問(wèn)微信接口需要)
public static string accesstoken(string WeChatWxAppId, string WeChatWxAppSecret)
{
 string strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", WeChatWxAppId, WeChatWxAppSecret));
 if (strJson.IndexOf("errcode") == -1)
 {
  return GetJsonValue(strJson, "access_token");
 }
 else
 {
  return "";
 }
}
//解析json
public static string GetJsonValue(string jsonStr, string key)
{
 string result = string.Empty;
 if (!string.IsNullOrEmpty(jsonStr))
 {
  key = "\"" + key.Trim('"') + "\"";
  int index = jsonStr.IndexOf(key) + key.Length + 1;
  if (index > key.Length + 1)
  {
   //先截逗號(hào),若是最后一個(gè),截“}”號(hào),取最小值
   int end = jsonStr.IndexOf(',', index);
   if (end == -1)
   {
    end = jsonStr.IndexOf('}', index);
   }
   result = jsonStr.Substring(index, end - index);
   result = result.Trim(new char[] { '"', ' ', '\'' }); //過(guò)濾引號(hào)或空格
  }
 }
 return result;
}

//請(qǐng)求url
public static string RequestUrl(string url, string method="post")
{
 // 設(shè)置參數(shù)
 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 CookieContainer cookieContainer = new CookieContainer();
 request.CookieContainer = cookieContainer;
 request.AllowAutoRedirect = true;
 request.Method = method;
 request.ContentType = "text/html";
 request.Headers.Add("charset", "utf-8");
 //發(fā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù)
 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
 //直到request.GetResponse()程序才開(kāi)始向目標(biāo)網(wǎng)頁(yè)發(fā)送Post請(qǐng)求
 Stream responseStream = response.GetResponseStream();
 StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
 //返回結(jié)果網(wǎng)頁(yè)(html)代碼
 string content = sr.ReadToEnd();
 return content;
}
//接收微信返回code
//接收微信數(shù)據(jù)獲取用戶(hù)信息
public ActionResult RedirectWeChat(string code, string state)
{
 if (string.IsNullOrEmpty(code))
 {
  return Content("您拒絕了授權(quán)!");
 }
 string access_token = accesstoken(微信AppId, 微信AppSecret);
 string st = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + 微信AppId + "&secret=" + 微信AppSecret + "&code=" + code + "&grant_type=authorization_code";
 string data = RequestUrl(st);
//拿到用戶(hù)openid
string openid=GetJsonValue(data, "openid");
//獲取用戶(hù)其他信息
 string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
 data = RequestUrl(url);
string subscribe=GetJsonValue(data, "subscribe");
 if (subscribe == "0")
 {
  ///未關(guān)注
  return RedirectToAction("");
 }

 return RedirectToAction("");
}

//發(fā)送紅包Action
public ActionResult HB()
{
 string openid = "";//用戶(hù)openid
 string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; 
 string orderNo = 商戶(hù)號(hào) + DateTime.Now.ToString("yyyymmdd")+"隨機(jī)10位數(shù)字";//商戶(hù)訂單號(hào) 組成:mch_id+yyyymmdd+10位一天內(nèi)不能重復(fù)的數(shù)字。 
 string Code = ""http://32為隨機(jī)字符串; string key="key=" + "";//支付密鑰(在商戶(hù)平臺(tái)設(shè)置32為字符串) 
 Dictionary<string, string> data = new Dictionary<string, string>(); data.Add("act_name", "");//活動(dòng)名稱(chēng) 
 data.Add("client_ip", "192.168.1.1");//Ip地址 
 data.Add("mch_billno", orderNo);//商戶(hù)訂單號(hào) 組成:mch_id+yyyymmdd+10位一天內(nèi)不能重復(fù)的數(shù)字。 
 data.Add("mch_id", "");//商戶(hù)號(hào) 
 data.Add("nonce_str", Code);//隨機(jī)字符串 
 data.Add("re_openid", openid);//用戶(hù)openid 
 data.Add("remark", "");//備注 
 data.Add("send_name","");//商戶(hù)名稱(chēng) 
 data.Add("total_amount", "100");//付款金額 單位分 
 data.Add("total_num", "1");//紅包發(fā)放總?cè)藬?shù) 
 data.Add("wishing", "恭喜發(fā)財(cái)");//紅包祝福語(yǔ) 
 data.Add("wxappid", );//公眾賬號(hào)appid 
 string xml = GetXML(data, key);//簽名+拼接xml 
 string str=PostWebRequests(url, xml);//微信返回xml err_code=SUCCESS 就是成功
 return View(""); 
}

//發(fā)送紅包(MD5簽名+拼接X(jué)ML)
public static string GetXML(Dictionary<string, string> data,string paykey)
{
 string retStr;
 MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();

 var data1=from d in data orderby d.Key select d;
 string data2 = "";
 string XML = "<xml>";
 foreach (var item in data1)
 {
  //空值不參與簽名
  if (item.Value + "" != "")
  {
   data2 += item.Key +"="+ item.Value + "&";
  }
  XML += "<" + item.Key + ">" + item.Value+""+ "</" + item.Key + ">";
 }

 data2 += paykey;
 //創(chuàng)建md5對(duì)象
 byte[] inputBye;
 byte[] outputBye;

 //使用GB2312編碼方式把字符串轉(zhuǎn)化為字節(jié)數(shù)組.
 try
 {
  inputBye = Encoding.UTF8.GetBytes(data2);
 }
 catch
 {
  inputBye = Encoding.GetEncoding("GB2312").GetBytes(data2);
 }
 outputBye = m5.ComputeHash(inputBye);

 retStr = System.BitConverter.ToString(outputBye);
 retStr = retStr.Replace("-", "").ToUpper();
 XML += "<sign>" + retStr + "</sign>";//簽名
 XML += "</xml>";
 return XML;
}

//發(fā)送紅包請(qǐng)求Post方法
public static string PostWebRequests(string postUrl, string menuInfo)
{
 string returnValue = string.Empty;
 try
 {
  Encoding encoding = Encoding.UTF8;
  byte[] bytes = encoding.GetBytes(menuInfo);
  string cert = @"E:\cdcert\apiclient_cert.p12";//支付證書(shū)路徑
  string password = "1212121";//支付證書(shū)密碼

  ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  X509Certificate cer = new X509Certificate(cert, password, X509KeyStorageFlags.MachineKeySet);
  HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(postUrl);
  webrequest.ClientCertificates.Add(cer);
  webrequest.Method = "post";
  webrequest.ContentLength = bytes.Length;
  webrequest.GetRequestStream().Write(bytes, 0, bytes.Length);
  HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();
  Stream stream = webreponse.GetResponseStream();
  string resp = string.Empty;
  using (StreamReader reader = new StreamReader(stream))
  {
    return reader.ReadToEnd();
  }

 }
 catch (Exception ex)
 {
  return "";
 }
} 

以下是微信開(kāi)發(fā)官方相關(guān)文檔

1. 【微信支付】公眾號(hào)支付開(kāi)發(fā)者文檔
2. 微信開(kāi)放平臺(tái)
3.企業(yè)號(hào)開(kāi)發(fā)者接口文檔
4.微信公眾平臺(tái)開(kāi)發(fā)者文檔

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • asp.net下計(jì)算數(shù)字1至10的總和

    asp.net下計(jì)算數(shù)字1至10的總和

    老師布置給Insus.NET第四道題目。一開(kāi)始時(shí),是使用下面的方法解答
    2012-05-05
  • mvc下,3種窗口彈出設(shè)置的方法

    mvc下,3種窗口彈出設(shè)置的方法

    想做頁(yè)面美化,特別是在一個(gè)頁(yè)面中,只占了很小一塊的頁(yè)面,想做成彈出窗口樣式,稍微總結(jié)了下:
    2013-07-07
  • Visual Studio 2015安裝步驟詳解

    Visual Studio 2015安裝步驟詳解

    這篇文章主要為大家詳細(xì)介紹了Visual Studio 2015安裝步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • ASP.NET 2.0 程序安全的基礎(chǔ)知識(shí)

    ASP.NET 2.0 程序安全的基礎(chǔ)知識(shí)

    成員關(guān)系的概念在人類(lèi)社會(huì)中是一個(gè)層次比較低的概念,源于希望屬于某個(gè)群組的意識(shí)。同樣,在A(yíng)SP.NET 2.0程序開(kāi)始開(kāi)發(fā)涉及到成員關(guān)系的應(yīng)用程序時(shí),必須首先理解身份、驗(yàn)證和授權(quán)這幾個(gè)關(guān)鍵的概念。
    2010-04-04
  • ASP.Net?Core中的日志與分布式鏈路追蹤

    ASP.Net?Core中的日志與分布式鏈路追蹤

    這篇文章介紹了ASP.Net?Core中的日志與分布式鏈路追蹤,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下<BR>
    2022-03-03
  • asp.net URL中包含中文參數(shù)造成亂碼的解決方法

    asp.net URL中包含中文參數(shù)造成亂碼的解決方法

    中文亂碼一直以來(lái)是WEB開(kāi)發(fā)中比較常見(jiàn)的問(wèn)題之一,對(duì)于初學(xué)者來(lái)說(shuō),各種各樣的編碼方式可能會(huì)有點(diǎn)不適應(yīng),本篇文章并不講述這些編碼,而是把自己遇到的一個(gè)小問(wèn)題以及該問(wèn)題的解決之法說(shuō)明一下,希望對(duì)大家有用。
    2010-03-03
  • asp.net用url重寫(xiě)URLReWriter實(shí)現(xiàn)任意二級(jí)域名

    asp.net用url重寫(xiě)URLReWriter實(shí)現(xiàn)任意二級(jí)域名

    Asp.net 用url重寫(xiě)(URLReWriter)實(shí)現(xiàn)任意二級(jí)域名
    2008-10-10
  • 調(diào)試ASP.NET2005/2008時(shí),端口不正確的解決三套方案

    調(diào)試ASP.NET2005/2008時(shí),端口不正確的解決三套方案

    這篇文章主要介紹了調(diào)試ASP.NET2005/2008時(shí),端口不正確的解決三套方案,小編就特別喜歡收藏這類(lèi)文章,方便以后工作學(xué)習(xí)中遇到這類(lèi)問(wèn)題進(jìn)行解決。
    2015-09-09
  • ASP.NET GridView 實(shí)現(xiàn)課程表顯示(動(dòng)態(tài)合并單元格)實(shí)現(xiàn)步驟

    ASP.NET GridView 實(shí)現(xiàn)課程表顯示(動(dòng)態(tài)合并單元格)實(shí)現(xiàn)步驟

    GridView,ASP.NET中很常用的數(shù)據(jù)顯示控件,這里,我將用這個(gè)控件來(lái)實(shí)現(xiàn)課程表的顯示。首先說(shuō)說(shuō)課程表的顯示與普通記錄的顯示有何不同?感興趣的朋友可以了解下,或許對(duì)你有所幫助
    2013-02-02
  • ASP.NET Core 使用Cookie驗(yàn)證身份的示例代碼

    ASP.NET Core 使用Cookie驗(yàn)證身份的示例代碼

    這篇文章主要介紹了ASP.NET Core 使用Cookie驗(yàn)證身份的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02

最新評(píng)論