public static Dictionary <string, object> get_user_info(string access_token, string open_id) { OAuthConfig oAuthConfig = OAuthConfig.GetOAuthConfig("qq"); string strUrl = string.Concat(new string[] { "https://graph.qq.com/user/get_user_info?access_token=", access_token, "&oauth_consumer_key=", oAuthConfig.OAuthAppId, "&openid=", open_id }); string text = NetWorkUtils.HttpGet(strUrl); Dictionary <string, object> result; if (text.Contains("error")) { result = null; } else { Dictionary <string, object> dictionary = JsonMapper.ToObject <Dictionary <string, object> >(text); result = dictionary; } return(result); }
public static Dictionary <string, object> get_token_info(string access_token) { string strUrl = "https://api.weibo.com/oauth2/get_token_info"; string postData = "access_token=" + access_token; string text = NetWorkUtils.HttpPost(strUrl, postData); Dictionary <string, object> result; if (text.Contains("error")) { result = null; } else { try { Dictionary <string, object> dictionary = JsonMapper.ToObject <Dictionary <string, object> >(text); result = dictionary; } catch { result = null; } } return(result); }
public static JsonData get_info(string access_token, string open_id) { string strUrl = "https://api.weibo.com/2/users/show.json?access_token=" + access_token + "&uid=" + open_id; string text = NetWorkUtils.HttpGet(strUrl); JsonData result; if (text.Contains("error")) { result = null; } else { try { JsonData jsonData = JsonMapper.ToObject(text); if (jsonData.Count > 0) { result = jsonData; return(result); } } catch { result = null; return(result); } result = null; } return(result); }
public static Dictionary <string, object> get_access_token(string code, string state) { OAuthConfig oAuthConfig = OAuthConfig.GetOAuthConfig("qq"); string strUrl = string.Concat(new string[] { "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=", oAuthConfig.OAuthAppId, "&client_secret=", oAuthConfig.OAuthAppKey, "&code=", code, "&state=", state, "&redirect_uri=", HttpContext.Current.Server.UrlEncode(oAuthConfig.ReturnUri) }); string text = NetWorkUtils.HttpGet(strUrl); Dictionary <string, object> result; if (text.Contains("error")) { result = null; } else { try { string[] array = text.Split(new char[] { '&' }); string value = array[0].Split(new char[] { '=' })[1]; string value2 = array[1].Split(new char[] { '=' })[1]; result = new Dictionary <string, object> { { "access_token", value }, { "expires_in", value2 } }; } catch { result = null; } } return(result); }
public void TestNetwork() { var getStr = NetWorkUtils.HttpGet("http://www.singoo.top"); Console.WriteLine("get返回数据:" + getStr); var postStr = NetWorkUtils.HttpPost("http://passports.singoo.top/passports/login", "_loginname=admin&_loginpwd=123"); Console.WriteLine("post返回数据:" + postStr); }
public static string Get(string strKey, string strExpressCompanyCode, string strExpressNo) { return(NetWorkUtils.HttpGet(string.Concat(new string[] { "http://www.kuaidi100.com/applyurl?key=", strKey, "&com=", strExpressCompanyCode, "&nu=", strExpressNo }))); }
public static string GetJSON() { string result; try { result = NetWorkUtils.HttpGet("http://www.ue.net.cn/api/uenews.json"); } catch { result = string.Empty; } return(result); }
public static Dictionary <string, object> get_open_id(string access_token) { string strUrl = "https://graph.qq.com/oauth2.0/me?access_token=" + access_token; string text = NetWorkUtils.HttpGet(strUrl); Dictionary <string, object> result; if (text.Contains("error")) { result = null; } else { int num = text.IndexOf('(') + 1; int num2 = text.LastIndexOf(')') - 1; text = text.Substring(num, num2 - num); Dictionary <string, object> dictionary = JsonMapper.ToObject <Dictionary <string, object> >(text); result = dictionary; } return(result); }
public static JsonData get_info(string access_token, string open_id) { OAuthConfig oAuthConfig = OAuthConfig.GetOAuthConfig("qq"); string strUrl = string.Concat(new string[] { "https://graph.qq.com/user/get_info?access_token=", access_token, "&oauth_consumer_key=", oAuthConfig.OAuthAppId, "&openid=", open_id }); string text = NetWorkUtils.HttpGet(strUrl); JsonData result; if (text.Contains("error")) { result = null; } else { try { JsonData jsonData = JsonMapper.ToObject(text); if (jsonData.Count > 0) { result = jsonData; return(result); } } catch { result = null; return(result); } result = null; } return(result); }
public static Dictionary <string, object> get_access_token(string code) { OAuthConfig oAuthConfig = OAuthConfig.GetOAuthConfig("sina"); string strUrl = "https://api.weibo.com/oauth2/access_token"; string postData = string.Concat(new string[] { "grant_type=authorization_code&code=", code, "&client_id=", oAuthConfig.OAuthAppId, "&client_secret=", oAuthConfig.OAuthAppKey, "&redirect_uri=", HttpContext.Current.Server.UrlEncode(oAuthConfig.ReturnUri) }); string text = NetWorkUtils.HttpPost(strUrl, postData); Dictionary <string, object> result; if (text.Contains("error")) { result = null; } else { try { Dictionary <string, object> dictionary = JsonMapper.ToObject <Dictionary <string, object> >(text); result = dictionary; } catch { result = null; } } return(result); }
public void TestPing() { Assert.AreEqual(true, NetWorkUtils.Ping("www.baidu.com")); Assert.AreEqual(true, NetWorkUtils.Ping("103.235.46.39")); }
private void LoadFromServer() { AccessTokenResult token = CommonApi.GetToken(SinGooCMS.Weixin.Config.AppID, SinGooCMS.Weixin.Config.AppSecret, "client_credential"); string strUrl = string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", token.access_token); string strValue = NetWorkUtils.HttpGet(strUrl); GetMenuResultFull getMenuResultFull = JsonUtils.JsonToObject <GetMenuResultFull>(strValue); if (getMenuResultFull != null && getMenuResultFull.menu != null && getMenuResultFull.menu.button.Count > 0) { WxMenu.EmptyLocal(); int num = 1; foreach (MenuFull_RootButton current in getMenuResultFull.menu.button) { WxMenuInfo wxMenuInfo = new WxMenuInfo { RootID = 0, ParentID = 0, Type = (current.type ?? string.Empty), Name = current.name, EventKey = (current.key ?? string.Empty), Url = (current.url ?? string.Empty), ChildCount = 0, ChildIDs = string.Empty, Sort = num, AutoTimeStamp = System.DateTime.Now }; int num2 = WxMenu.Add(wxMenuInfo); if (num2 > 0) { wxMenuInfo.AutoID = num2; wxMenuInfo.RootID = num2; num++; if (current.sub_button != null && current.sub_button.Count > 0) { wxMenuInfo.ChildCount = current.sub_button.Count; int num3 = 1; foreach (MenuFull_RootButton current2 in current.sub_button) { WxMenuInfo entity = new WxMenuInfo { RootID = num2, ParentID = num2, Type = (current2.type ?? string.Empty), Name = current2.name, EventKey = (current2.key ?? string.Empty), Url = (current2.url ?? string.Empty), ChildCount = 0, ChildIDs = string.Empty, Sort = num3, AutoTimeStamp = System.DateTime.Now }; int num4 = WxMenu.Add(entity); if (num4 > 0) { WxMenuInfo expr_27E = wxMenuInfo; expr_27E.ChildIDs = expr_27E.ChildIDs + num4 + ","; num3++; } } } if (wxMenuInfo.ChildIDs.EndsWith(",")) { wxMenuInfo.ChildIDs = wxMenuInfo.ChildIDs.TrimEnd(new char[] { ',' }); } WxMenu.Update(wxMenuInfo); } } } this.BindData(); }
private async void Work(object sender, ElapsedEventArgs e) { if (Conf == null) { return; } timer.Interval = Conf.Interval * 60 * 1000; if (Interlocked.Read(ref runFlag) == 0) { return; } Log.Info($"[{Name}] do work ..."); try { //获取本机公网IP string realIp = ""; if (Conf.GetIpUrls.Count == 0 && Conf.IsIpV6) { //IPCONFIG realIp = NetWorkUtils.GetLocalIpV6Address(); } if (Conf.GetIpUrls.Count == 0 && !Conf.IsIpV6) { Conf.GetIpUrls.Add(DEFAULT_IP_V4_URL); } if (Conf.GetIpUrls.Count > 0) { foreach (string url in Conf.GetIpUrls) { var getRes = await url.Get(); if (!getRes.Ok) { Log.Info($"[{Name}] fetch real internet ip from {url} fail , try next url"); continue; } Match mc; //提取IPV6地址 if (Conf.IsIpV6) { mc = IpV6Regex.Match(getRes.HttpResponseString); } //提取IPV4地址 else { mc = IpV4Regex.Match(getRes.HttpResponseString); } if (mc.Success && mc.Groups.Count > 0) { realIp = mc.Groups[0].Value; Log.Info( $"[{Name}] fetch real internet ip from ( {url} ) success, current ip is ( {realIp} )"); break; } } } if (string.IsNullOrWhiteSpace(realIp)) { Log.Info($"[{Name}] fetch real internet ip all failed, skip"); return; } // double check if (!realIp.IsIpAddress()) { Log.Info($"[{Name}] fetch real internet ip [{realIp}] is not a valid ip address, skip"); return; } //double check if (Conf.IsIpV6 && !realIp.IsIpV6Address()) { Log.Info($"[{Name}] fetch real internet ip [{realIp}] is not a valid ipv6 address, skip"); return; } //如果上一次ip跟本次获取到的ip是相同的,则退出 hhfei 2020-06-19 if (realIp.Equals(lastIP)) { return; } lastIP = realIp; foreach (string subDomainName in Conf.SubDomainNames) { //获取阿里云记录 var describeRes = await new DescribeDomainRecordsRequest(Conf.AccessKeyId, Conf.AccessKeySecret) { DomainName = Conf.DomainName, RRKeyWord = subDomainName, TypeKeyWord = Conf.Type, }.Execute(); if (describeRes.HasError) { Log.Info($"[{this.GetName(subDomainName)}] describe domain records fail ( {describeRes.Message} ) , skip"); return; } //未查到记录,添加 if (describeRes.TotalCount == 0) { //add Log.Info($"[{this.GetName(subDomainName)}] prepare to add domain record ..."); var addRes = await new AddDomainRecordRequest( Conf.AccessKeyId, Conf.AccessKeySecret) { DomainName = Conf.DomainName, RR = subDomainName, Type = Conf.Type, Value = realIp, TTL = Conf.TtlV, Line = Conf.Line, }.Execute(); Log.Info(addRes.HasError ? $"[{this.GetName(subDomainName)}] add domain record fail ( {addRes.Message} ) , skip" : $"[{this.GetName(subDomainName)}] add domain record ok , now record value is {realIp}"); } else { foreach (var record in describeRes.DomainRecords.Records) { //fix https://github.com/xuchao1213/AliyunDdnsCSharp/issues/3 //fix https://github.com/xuchao1213/AliyunDdnsCSharp/issues/11 if (string.Compare(record.Type, Conf.Type, StringComparison.OrdinalIgnoreCase) == 0 && string.Compare(record.RR, subDomainName, StringComparison.OrdinalIgnoreCase) == 0) { if (record.Value == realIp) { Log.Info($"[{this.GetName(subDomainName)}] ip not chanage , skip"); break; } // //update Log.Info($"[{this.GetName(subDomainName)}] prepare to update domain record ..."); var updateRes = await new UpdateDomainRecordRequest( Conf.AccessKeyId, Conf.AccessKeySecret) { RecordId = record.RecordId, RR = subDomainName, Type = Conf.Type, Value = realIp, TTL = Conf.TtlV, Line = Conf.Line, }.Execute(); Log.Info(updateRes.HasError ? $"[{this.GetName(subDomainName)}] update domain record fail ( {updateRes.Message} ) , skip" : $"[{this.GetName(subDomainName)}] update domain record ok , now record value is {realIp}"); if (updateRes.HasError) { break; } //更新成功后,暂停解析->启用解析,以此来解决更新后不立即生效的问题 var disableRes = await new SetDomainRecordStatusRequest( Conf.AccessKeyId, Conf.AccessKeySecret) { RecordId = updateRes.RecordId, Enable = false }.Execute(); Log.Info(disableRes.HasError ? $"[{this.GetName(subDomainName)}] set domain records status to disable error ( {disableRes.Message} ) , skip" : $"[{this.GetName(subDomainName)}] set domain records status to disable ok , now enable it"); if (disableRes.HasError) { break; } var enableRes = await new SetDomainRecordStatusRequest( Conf.AccessKeyId, Conf.AccessKeySecret) { RecordId = updateRes.RecordId, Enable = true }.Execute(); Log.Info(enableRes.HasError ? $"[{this.GetName(subDomainName)}] set domain records status to enable error ( {enableRes.Message} ) , skip" : $"[{this.GetName(subDomainName)}] set domain records status to enable ok , just enjoy it :)"); } } } } } catch (Exception ex) { Log.Warn($"[{Name}] do work exception : {ex.Message}"); } }
private async void Work(object sender, ElapsedEventArgs e) { if (conf == null) { return; } timer.Interval = conf.Interval * 60 * 1000; if (Interlocked.Read(ref runFlag) == 0) { return; } Log.Info($"[{Name}] do work ..."); try { //获取本机公网IP string realIp = ""; if (conf.GetIpUrls.Count == 0 && conf.IsIpV6) { //IPCONFIG realIp = NetWorkUtils.GetLocalIpV6Address(); } if (conf.GetIpUrls.Count == 0 && !conf.IsIpV6) { conf.GetIpUrls.Add(DEFAULT_IP_V4_URL); } if (conf.GetIpUrls.Count > 0) { foreach (string url in conf.GetIpUrls) { var getRes = await url.Get(); if (!getRes.Ok) { Log.Info($"[{Name}] fetch real internet ip from {url} fail , try next url"); continue; } Match mc; //提取IPV6地址 if (conf.IsIpV6) { mc = IpV6Regex.Match(getRes.HttpResponseString); } //提取IPV4地址 else { mc = IpV4Regex.Match(getRes.HttpResponseString); } if (mc.Success && mc.Groups.Count > 0) { realIp = mc.Groups[0].Value; Log.Info($"[{Name}] fetch real internet ip from ( {url} ) success, current ip is ( {realIp} )"); break; } } } if (string.IsNullOrWhiteSpace(realIp)) { Log.Info($"[{Name}] fetch real internet ip all failed, skip"); return; } // double check if (!realIp.IsIpAddress()) { Log.Info($"[{Name}] fetch real internet ip [{realIp}] is not a valid ip address, skip"); return; } //double check if (conf.IsIpV6 && !realIp.IsIpV6Address()) { Log.Info($"[{Name}] fetch real internet ip [{realIp}] is not a valid ipv6 address, skip"); return; } //获取阿里云记录 var describeRes = await new DescribeDomainRecordsRequest(conf.AccessKeyId, conf.AccessKeySecret) { DomainName = conf.DomainName, RRKeyWord = conf.SubDomainName, TypeKeyWord = conf.Type, }.Execute(); if (describeRes.HasError) { Log.Info($"[{Name}] describe domain records fail ( {describeRes.Message} ) , skip"); return; } //未查到记录,添加 if (describeRes.TotalCount == 0) { goto ADD; } //数量多于1个时删除所有匹配的重新添加 else if (describeRes.TotalCount > 1) { foreach (var record in describeRes.DomainRecords.Records) { var deleteRes = await new DeleteDomainRecordRequest(conf.AccessKeyId, conf.AccessKeySecret) { RecordId = record.RecordId }.Execute(); if (deleteRes.HasError) { Log.Info($"[{Name}] delete domain records fail ( {deleteRes.Message} ) , skip"); continue; } Log.Info($"[{Name}] delete domain records sucess"); } //重新添加 goto ADD; } else { RecordDetail record = describeRes.DomainRecords.Records[0]; if (record.Value == realIp) { Log.Info($"[{Name}] ip not chanage , skip"); return; } //update Log.Info($"[{Name}] prepare to update domain record ..."); var updateRes = await new UpdateDomainRecordRequest( conf.AccessKeyId, conf.AccessKeySecret) { RecordId = record.RecordId, RR = conf.SubDomainName, Type = conf.Type, Value = realIp, TTL = conf.TtlV, Line = conf.Line, }.Execute(); Log.Info(updateRes.HasError ? $"[{Name}] update domain record fail ( {updateRes.Message} ) , skip" : $"[{Name}] update domain record ok , now record value is {realIp}"); if (updateRes.HasError) { return; } //更新成功后,暂停解析->启用解析,以此来解决更新后不立即生效的问题 var disableRes = await new SetDomainRecordStatusRequest( conf.AccessKeyId, conf.AccessKeySecret) { RecordId = record.RecordId, Enable = false }.Execute(); Log.Info(disableRes.HasError ? $"[{Name}] set domain records tatus to diable error ( {disableRes.Message} ) , skip" : $"[{Name}] set domain records tatus to diable ok , now enable it"); if (disableRes.HasError) { return; } var enableRes = await new SetDomainRecordStatusRequest( conf.AccessKeyId, conf.AccessKeySecret) { RecordId = record.RecordId, Enable = true }.Execute(); Log.Info(enableRes.HasError ? $"[{Name}] set domain records tatus to enable error ( {enableRes.Message} ) , skip" : $"[{Name}] set domain records tatus to enable ok , just enjoy it :)"); return; } ADD: { //add Log.Info($"[{Name}] prepare to add domain record ..."); var addRes = await new AddDomainRecordRequest( conf.AccessKeyId, conf.AccessKeySecret) { DomainName = conf.DomainName, RR = conf.SubDomainName, Type = conf.Type, Value = realIp, TTL = conf.TtlV, Line = conf.Line, }.Execute(); Log.Info(addRes.HasError ? $"[{Name}] add domain record fail ( {addRes.Message} ) , skip" : $"[{Name}] add domain record ok , now record value is {realIp}"); } } catch (Exception ex) { Log.Warn($"[{ Name}] do work exception : {ex.Message}"); } }