コード例 #1
0
        /// <summary>
        /// 用code 获取accesstoken,获取用户信息
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="clientsecret"></param>
        /// <param name="redirecturl">重定向地址,</param>
        /// <param name="Code">code</param>
        /// <returns>返回用户信息</returns>
        public string GetTokenInfo(string clientId, string clientsecret, string GetTokenUrl, string Code)
        {
            try
            {
                WriteLogHelper.WriteLogDoc("oauthlog", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + GetTokenUrl + "," + clientsecret + "," + Code, "oauthlog");
                string url    = string.Format(GetTokenUrl, clientId, clientsecret, Code);
                string ReText = CommonMethod.GetHttpResponse(url, 1200);//post/get方法获取信息

                WriteLogHelper.WriteLogDoc("oauthlog", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + ReText, "oauthlog");

                Dictionary <string, object> DicText = JsonConvert.DeserializeObject <Dictionary <string, object> >(ReText);


                if (!DicText.ContainsKey("access_token"))
                {
                    return("获取access_token失败");
                }
                else
                {
                    WriteLogHelper.WriteLogDoc("oauthlog", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + DicText["user"] + DicText["access_token"], "oauthlog");

                    return(DicText["access_token"].ToString());
                }
            }
            catch (Exception ex)
            {
                WriteLogHelper.WriteLogDoc("oauthlog", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + ex.Message, "oauthlog");
                return("");
            }
        }
コード例 #2
0
        public static string GetHttpResponse(string url, int Timeout)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method      = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                request.UserAgent   = null;
                request.Timeout     = 12000;

                HttpWebResponse response         = (HttpWebResponse)request.GetResponse();
                Stream          myResponseStream = response.GetResponseStream();
                StreamReader    myStreamReader   = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string          retString        = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                return(retString);
            }
            catch (Exception ex)
            {
                //   return ex.Message;//url错误时候回报错
                WriteLogHelper.WriteLogDoc("GetHttpResponse", "异常:" + ex.Message, "HttpLog");
                return(ex.Message);
            }
        }
コード例 #3
0
 /// <summary>
 /// GET请求
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="url"></param>
 /// <returns></returns>
 public static T GetResponse <T>(string url) where T : class, new()
 {
     try {
         string         returnValue = string.Empty;
         HttpWebRequest webReq      = (HttpWebRequest)WebRequest.Create(new Uri(url));
         webReq.Method      = "GET";
         webReq.ContentType = "application/json";
         using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
         {
             using (StreamReader streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
             {
                 returnValue = streamReader.ReadToEnd();
                 T result = default(T);
                 result = JsonConvert.DeserializeObject <T>(returnValue);
                 return(result);
             }
         }
     }
     catch (Exception ex)
     {
         //   return ex.Message;//url错误时候回报错
         WriteLogHelper.WriteLogDoc("GetResponse", "异常:" + ex.Message, "HttpLog");
         return(null);
     }
 }
コード例 #4
0
        /// <summary>
        /// 获取用户code
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="redirect_uri">重定向url</param>
        public void GetCodeUrl(string clientId, string getcodeurl, string redirect_uri)
        {
            // string url = string.Format(" http://api.eshimin.com/api/oauth/authorize?client_id={0}&response_type=code&redirect_uri={1}&scope=read", clientId, redirect_uri);
            string url = string.Format(getcodeurl, clientId, redirect_uri);

            CommonMethod.WebRequestPostOrGet(url, "");
            WriteLogHelper.WriteLogDoc("oauthlog", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + url, "oauthlog");
        }
コード例 #5
0
        /// <summary>
        /// Post/get 提交调用抓取
        /// </summary>
        /// <param name="url">提交地址</param>
        /// <param name="param">参数</param>
        /// <returns>string</returns>
        public static string WebRequestPostOrGet(string sUrl, string sParam)
        {
            byte[] bt = System.Text.Encoding.UTF8.GetBytes(sParam);

            Uri            uriurl = new Uri(sUrl);
            HttpWebRequest req    = (HttpWebRequest)HttpWebRequest.Create(uriurl);//HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url + (url.IndexOf("?") > -1 ? "" : "?") + param);

            req.Method        = "POST";
            req.Timeout       = 120 * 1000;
            req.ContentType   = "application/x-www-form-urlencoded;";
            req.ContentLength = bt.Length;

            using (Stream reqStream = req.GetRequestStream())//using 使用可以释放using段内的内存
            {
                reqStream.Write(bt, 0, bt.Length);
                reqStream.Flush();
            }
            try
            {
                using (WebResponse res = req.GetResponse())
                {
                    //在这里对接收到的页面内容进行处理

                    Stream resStream = res.GetResponseStream();

                    StreamReader resStreamReader = new StreamReader(resStream, System.Text.Encoding.UTF8);

                    string resLine;

                    WriteLogHelper.WriteLogDoc("WebRequestPostOrGet", "" + resStreamReader + req, "HttpLog");
                    System.Text.StringBuilder resStringBuilder = new System.Text.StringBuilder();

                    while ((resLine = resStreamReader.ReadLine()) != null)
                    {
                        resStringBuilder.Append(resLine + System.Environment.NewLine);
                    }

                    resStream.Close();
                    resStreamReader.Close();

                    return(resStringBuilder.ToString());
                }
            }
            catch (Exception ex)
            {
                //   return ex.Message;//url错误时候回报错
                WriteLogHelper.WriteLogDoc("WebRequestPostOrGet", "异常:" + ex.Message, "HttpLog");
                return(ex.Message);
            }
        }
コード例 #6
0
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="accesstoken"></param>
        /// <param name="GetUserInfoUrl"></param>
        /// <returns></returns>
        public Dictionary <string, object> GetUserInfo(string accesstoken, string GetUserInfoUrl)
        {
            try
            {
                string url = string.Format(GetUserInfoUrl, accesstoken);

                WriteLogHelper.WriteLogDoc("oauthlog", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + url, "oauthlog");

                string returnText = CommonMethod.GetHttpResponse(url, 1200);
                Dictionary <string, object> DicText = JsonConvert.DeserializeObject <Dictionary <string, object> >(returnText);

                return(DicText);
            }
            catch (Exception ex)
            {
                WriteLogHelper.WriteLogDoc("oauthlog", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + ex.Message, "oauthlog");
                return(null);
            }
        }