使用 HttpWebRequest 进行基本身份验证



我正在制作一个类似邮递员的应用程序,我正在尝试使用 HttpWebRequest 实现基本身份验证,但我总是以403 forbidden error结尾,我想这意味着我做错了什么。

我目前正在使用WebHeaderCollection来存储我的标头。我像这样构建我的授权标头:

法典:

private WebHeaderCollection AddAuthorization(WebHeaderCollection wc)
    {
        if (expanderBasique.IsExpanded && !String.IsNullOrEmpty(BasiqueUserName.Text) && !String.IsNullOrEmpty(BasiquePassword.Password))
            {
                String username = BasiqueUserName.Text.Trim();
                String password = BasiquePassword.Password.Trim();
                String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
                wc.Add("Authorization", "Basic " + encoded);
            }
            return wc;
        }

然后我将我的集合链接到 HttpWebRequest:

法典:

private async Task<WebResponse> ExecuteHttpWebRequest()
        {
            var selectedMethode = Methode.SelectedItem as TextBlock;
            string method = selectedMethode.Text;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Query.Text);
            WebHeaderCollection myWebHeaderCollection = webRequest.Headers;
            myWebHeaderCollection = BuildHeaderCollection(myWebHeaderCollection);
            myWebHeaderCollection = AddAuthorization(myWebHeaderCollection);
            webRequest.PreAuthenticate = true;
            webRequest.Method = method;
            if (webRequest.Method == "POST" || webRequest.Method == "PUT" || webRequest.Method == "PATCH")
            {
                byte[] data = Encoding.ASCII.GetBytes(Body.Text);
                webRequest.ContentLength = data.Length;
                using (var stream = webRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            return await webRequest.GetResponseAsync();
        }

我使用自己的凭据使用 GitHub API 尝试此代码:https://api.github.com/user

使用邮递员或 restER 等其他应用程序,我得到了我想要的结果,但我总是以我自己的 403 错误告终。

我真的很想坚持使用HttpWebRequest。
我尝试了堆栈溢出的各种解决方案,但没有成功:这里或这里

编辑 :
根据FaizanRabbani的评论,我通过将UserAgent属性设置为httpwebrequest来解决问题:

法典:

private async Task<WebResponse> ExecuteHttpWebRequest()
        {
            var selectedMethode = Methode.SelectedItem as TextBlock;
            string method = selectedMethode.Text;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Query.Text);
            WebHeaderCollection myWebHeaderCollection = webRequest.Headers;
            myWebHeaderCollection = BuildHeaderCollection(myWebHeaderCollection);
            myWebHeaderCollection = AddAuthorization(myWebHeaderCollection);
            webRequest.PreAuthenticate = true;
            webRequest.Method = method;
            webRequest.UserAgent = "something";
            if (webRequest.Method == "POST" || webRequest.Method == "PUT" || webRequest.Method == "PATCH")
            {
                byte[] data = Encoding.ASCII.GetBytes(Body.Text);
                webRequest.ContentLength = data.Length;
                using (var stream = webRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            return await webRequest.GetResponseAsync();
        }

相关内容

  • 没有找到相关文章

最新更新