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();
                }
            }
  • 相关阅读:
    高阶篇:1.2)材料和工艺的选择
    高阶篇:4.1.1)QFDI(客户需求转换为设计要求)
    高阶篇:4)可靠性设计-总章
    高阶篇:1.5)如何选择更好的概念-Pugh矩阵法
    知识点篇:2)产品结构设计目标的分类
    高阶篇:1.1)竞品(标杆产品)的拆解和分析benchmarking
    支持向量机
    机器学习概述
    HDU_oj_2055 An easy problem
    HDU_oj_2054 A==B ?
  • 原文地址:https://www.cnblogs.com/scottckt/p/2270253.html
Copyright © 2011-2022 走看看