代码之家  ›  专栏  ›  技术社区  ›  Fraz Zaki

如何在c[副本]中配置httpwebrequest中的x身份验证

  •  0
  • Fraz Zaki  · 技术社区  · 6 年前

    我需要在 HttpWebRequest 对象。如何将自定义页眉添加到 httpwebrequest请求 对象。

    0 回复  |  直到 8 年前
        1
  •  144
  •   Anders Marzi Tornblad    8 年前

    你使用 Headers 具有字符串索引的属性:

    request.Headers["X-My-Custom-Header"] = "the-value";
    

    根据msdn,这是从:

    • 通用Windows平台4.5
    • .NET框架1.1
    • 可移植类库
    • Silverlight 2.0版
    • Windows Phone Silverlight 7.0版
    • Windows Phone 8.1版

    https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers(v=vs.110).aspx

        2
  •  16
  •   SharK    9 年前

    创建服务、添加头和读取json响应的简单方法,

    private static void WebRequest()
        {
            const string WEBSERVICE_URL = "<<Web service URL>>";
            try
            {
                var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
                if (webRequest != null)
                {
                    webRequest.Method = "GET";
                    webRequest.Timeout = 12000;
                    webRequest.ContentType = "application/json";
                    webRequest.Headers.Add("Authorization", "Basic dchZ2VudDM6cGFdGVzC5zc3dvmQ=");
    
                    using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                        {
                            var jsonResponse = sr.ReadToEnd();
                            Console.WriteLine(String.Format("Response: {0}", jsonResponse));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    
        3
  •  2
  •   Svarog    13 年前

    您可以将值添加到httpwebrequest.headers集合。

    根据msdn,它应该在windows phone中受支持: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers%28v=vs.95%29.aspx