使用NoAuth的Webrequest

时间:2018-07-31 09:18:16

标签: c# azure-webjobs

此刻,我正在创建一个WebRequest来连接到系统,但在Visual Studio中由于授权而被拒绝。如果我通过具有NoAuth设置的Postman使用相同的凭据,则它将连接。如何设置编程版本的凭据具有NoAuth?

private HttpWebRequest CreateWebRequest()
        {
            HttpWebRequest request = null;

            if (Reference != null && Password != null)
            {
                request = (HttpWebRequest)WebRequest.Create($"{Host}/{Path}");
                request.Method = "POST";
                request.ContentType = "application/xml";
                request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

#if DEBUG   //Required for the localproxy
                IWebProxy webProxy = WebRequest.DefaultWebProxy;
                webProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                request.Proxy = webProxy;
#endif

                var body = $"v={Version}&ref={Reference}&password={Password}";
                var data = Encoding.UTF8.GetBytes(body);
                request.ContentLength = data.Length;

                using (var requestStream = request.GetRequestStream())
                {
                    requestStream.Write(data, 0, data.Length);
                }
            }

            return request;
        }

1 个答案:

答案 0 :(得分:1)

当您选择“不验证”时,“邮递员”会做什么?它不会在请求中发送“授权”参数。如果您在请求标头中发送“授权”参数,则只需将其删除。