zoukankan      html  css  js  c++  java
  • Asp.net中编程方式调用ashx(通过webRequest)

    请看代码:

        public sealed string GetGscCurrentUser()
        {
            HttpWebRequest webRequest = null;
            StreamReader responseReader = null;
            try
            {
                //ashx Url
                string getGscUserUrl = "http:/xxx.com/GscHandler.ashx";
                //加入参数,用于更新请求
                string urlHandler = getGscUserUrl + "?id=" + Guid.NewGuid();            
                webRequest = (HttpWebRequest)HttpWebRequest.Create(urlHandler);
                webRequest.Timeout = 3000;//3秒超时
                
    //调用ashx,并取值
                responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
                string currentUserGulid = responseReader.ReadToEnd();
                return currentUserGulid.Trim();
            }
            catch
            {
                return "";
            }
            finally
            {
                responseReader.Close();
                responseReader.Dispose();
            }
        }

     需要授权时写法如下:

            public string GetGscCurrentUser()
            {
                HttpWebRequest webRequest = null;
                StreamReader responseReader = null;
                try
                {
                    string getGscUserUrl = System.Configuration.ConfigurationManager.AppSettings["GscGetUserUrl"];
                    string urlHandler = getGscUserUrl + "?id=" + Guid.NewGuid();
                    webRequest = (HttpWebRequest)HttpWebRequest.Create(urlHandler);
                    webRequest.Timeout = 3000;//3秒超时
                    webRequest.PreAuthenticate = true;
                    NetworkCredential gscCred = new NetworkCredential("account""***");
                    webRequest.Credentials = gscCred;

                    responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
                    string currentUserGulid = responseReader.ReadToEnd();
                    return currentUserGulid.Trim();
                }
                catch
                {
                    return "";
                }
                finally
                {
                    responseReader.Close();
                    responseReader.Dispose();
                }
            }
  • 相关阅读:
    阿牛的EOF牛肉串
    盐水的故事
    密码
    Digital Roots
    不容易系列之(3)—— LELE的RPG难题
    不容易系列之一
    超级楼梯
    母牛的故事
    蟠桃记
    Children’s Queue
  • 原文地址:https://www.cnblogs.com/scottckt/p/2270253.html
Copyright © 2011-2022 走看看