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();
                }
            }
  • 相关阅读:
    C语言提供的位运算符
    JAVA反射改动常量,以及其局限
    直击中关村创业大街,新街头霸王来了
    bind() to 0.0.0.0:80 failed (98: Address already in use)
    Eclipse 快捷方式 指定 固定 workspace
    C++对象模型——Inline Functions(第四章)
    eclipse中安装freemarker插件及ftl使用freemarker编辑器
    迷茫了好一阵决定做WEB前端
    ios代理的使用,正向传值,逆向传值
    easyUI Tab href,content差别
  • 原文地址:https://www.cnblogs.com/scottckt/p/2270253.html
Copyright © 2011-2022 走看看