[C#] 纯文本查看 复制代码
public string getOpenId(string Access_toKen)
{
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "https://graph.qq.com/oauth2.0/me?access_token=" + Access_toKen,//URL 必需项
Method = "GET",//URL 可选项 默认为Get
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = "",//字符串Cookie 可选项
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",//用户的浏览器类型,版本,操作系统 可选项有默认值
Accept = "text/html, application/xhtml+xml, */*",// 可选项有默认值
ContentType = "text/html",//返回类型 可选项有默认值
Referer = "https://graph.qq.com",//来源URL 可选项
Postdata = "",//Post数据 可选项GET时不需要写
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
string cookie = result.Cookie;
byte[] b = result.ResultByte;
try
{
JavaScriptSerializer js = new JavaScriptSerializer();
jsonData p = js.Deserialize<jsonData>(html);
//Response.Write("access_token:" + p.access_token.ToString() + ",expires_in:" + p.expires_in + "<br/>");//输出 access_token:0,expires_in:ZhangS
return p.openid;
}
catch (Exception e)
{
return e.ToString();
}
}
/// <summary>
/// 获取Access_toKen,并用Session保存
/// </summary>
/// <param name="appId"></param>
/// <param name="appSecret"></param>
public void getAccess_toKen(string appId, string appSecret)
{
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret,//URL 必需项
Method = "GET",//URL 可选项 默认为Get
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = "",//字符串Cookie 可选项
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",//用户的浏览器类型,版本,操作系统 可选项有默认值
Accept = "text/html, application/xhtml+xml, */*",// 可选项有默认值
ContentType = "text/html",//返回类型 可选项有默认值
Referer = "https://api.weixin.qq.com",//来源URL 可选项
Postdata = "",//Post数据 可选项GET时不需要写
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
string cookie = result.Cookie;
byte[] b = result.ResultByte;
JavaScriptSerializer js = new JavaScriptSerializer();
//return js.Deserialize<jsonData>(strJson);
//DeSerializer
jsonData p = js.Deserialize<jsonData>(html);
//Response.Write("access_token:" + p.access_token.ToString() + ",expires_in:" + p.expires_in + "<br/>");//输出 access_token:0,expires_in:ZhangS
Session["Session_Access_toKen"] = p.access_token;
//getOpenId(p.access_token);
}
/*订单发货or商家取消订单时调用的微信支付发货接口(只有付款方式为微信支付时才会调用)
* @param openId 买家支付时的微信openid
* @param transid 微站支付时交易流水号
* @param orderId 对应订单号
* @param status 发货状态 1表明发货成功 0表明发货失败(取消订单)
* @param msg 发货状态信息 发货成功填写ok即可 如果是取消订单则填写上取消订单原因(注意要进行UTF8编码哦)
* @param siteId 所属站点
* @author qlf
* @datetime 2014-4-23 上午11:32:31
*/
public void sendWxpayProduct(string openId, string transid, string orderId, string status, string msg)
{
string httpSend = "https://api.weixin.qq.com/pay/delivernotify?access_token=";
//System.out.println("发货接口Token:"+CommonUtil.getAccessToken(siteId));
httpSend += TenpayUtil.accss_token;
//System.out.println("请求地址:"+httpSend);
//商户订单号
string out_trade_no = "D_" + DateTime.Now.ToString("yyyy-MM-dd") + "_" + new System.Random().Next(0, 9999) + "-" + orderId;
//时间戳
string deliver_timestamp = Convert.ToString(DateTime.Now.Ticks / 1000);
//拼装加密字符串
string signStr = "appid=" + TenpayUtil.appid + "&appkey=" + TenpayUtil.appkey + "&deliver_msg=" + msg + "&deliver_status=" + status
+ "&deliver_timestamp=" + deliver_timestamp + "&openid=" + openId + "&out_trade_no=" + out_trade_no + "&transid=" + transid;
//System.out.println("签名字符串:"+signStr);
//生成加密字符串
string sign = SHA1Util.getSha1((signStr));
//System.out.println("MD5签名:"+sign);
//sign = SHA1Util.Sha1(sign);
//System.out.println("SHA1签名:"+sign);
//拼装json字符串
string json = "{\"appid\":\"" + TenpayUtil.appid + "\",\"openid\":\"" + openId + "\",\"transid\":\"" + transid + "\",\"out_trade_no\":\"" + out_trade_no + "\"," +
"\"deliver_timestamp\":\"" + deliver_timestamp + "\",\"deliver_status\":\"1\",\"deliver_msg\":\"ok\"," +
"\"app_signature\":\"" + sign + "\",\"sign_method\":\"shal\"}";
//System.out.println("发货接口json:"+json);
string respStr = "";
try
{
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "https://api.weixin.qq.com/pay/delivernotify?access_token=",//URL 必需项
Method = "GET",//URL 可选项 默认为Get
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = "",//字符串Cookie 可选项
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",//用户的浏览器类型,版本,操作系统 可选项有默认值
Accept = "text/html, application/xhtml+xml, */*",// 可选项有默认值
ContentType = "text/html",//返回类型 可选项有默认值
Referer = "https://api.weixin.qq.com",//来源URL 可选项
Postdata = "",//Post数据 可选项GET时不需要写
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
string cookie = result.Cookie;
byte[] b = result.ResultByte;
Response.Write(html);
}
catch (Exception e)
{
Response.Write(e.ToString());
}
//System.out.println("发货接口返回值="+respStr);
}