403使用Webrequest的禁止错误但在邮递员中有效 [英] 403 Forbidden error using Webrequest but works in postman

查看:71
本文介绍了403使用Webrequest的禁止错误但在邮递员中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个授权代码,我需要在调用api时使用一些标头值传入正文。当从邮递员那里尝试同样的工作时它的工作正常,但C#Webclient抛出403错误。

I have a Authorization code which i need to pass in body with some header value when calling a api. when trying the same from postman its working fine but C# Webclient throwing 403 error.

以下代码: -

    public string GetResponse(string AuthCode)

        {

   public string GetResponse(string AuthCode)
        {

    string url =" https://example.com//openam/oauth2/access_token?grant_type = authorization_code& realm = / cbpgatqa" ;;

            Uri uri = new Uri(string.Format(url));



            ASCIIEncoding encoding = new ASCIIEncoding();

            string postData =" code =" + AuthCode +"& redirect_uri =" +" http:// localhost:8080";

            byte [] data = Encoding.GetEncoding(" UTF-8")。GetBytes(postData);



            //创建请求

            var httpWebRequest =(HttpWebRequest)WebRequest.Create(uri);

            httpWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

            httpWebRequest.Headers.Add(HttpRequestHeader.Authorization," Basic" + AuthCode);

            httpWebRequest.ContentType =" application / json";

            httpWebRequest.Method =" POST";

            httpWebRequest.ContentLength = data.Length;

            httpWebRequest.UserAgent = @" Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 51.0.2704.106 Safari / 537.36" ;;



            Stream stream = httpWebRequest.GetRequestStream();

            stream.Write(data,0,data.Length);

            stream.Close();



            //获取回复

            HttpWebResponse httpResponse = null;

           试试
            {

                httpResponse =(HttpWebResponse)httpWebRequest.GetResponse();

            }¥b $ b            catch(例外情况)

            {

                Console.WriteLine("获取资源时的异常"+"ex.Message");

               返回null;

            }


            string result = null;

           使用(var streamReader = new StreamReader(httpResponse.GetResponseStream())))
            {

                result = streamReader.ReadToEnd();

            }


           返回结果;

    string url = "https://example.com//openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa";
            Uri uri = new Uri(string.Format(url));

            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "code=" + AuthCode + "&redirect_uri=" + "http://localhost:8080";
            byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(postData);

            // Create the request
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + AuthCode);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentLength = data.Length;
            httpWebRequest.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";

            Stream stream = httpWebRequest.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();

            // Get the response
            HttpWebResponse httpResponse = null;
            try
            {
                httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception while getting resource " + ex.Message);
                return null;
            }

            string result = null;
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }

            return result;

}

Postman Curl命令: -

Postman Curl command:-


从curl请求生成:

Generated from a curl request:


curl -X POST  

'https://example.com//openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa'<跨度>&NBSP;

-H '授权:基本MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc ='<跨度>&NBSP;

-H"缓存控制:无-cache' 

-H'内容类型:application / x-www-form-urlencoded' 

-d'code = 93317468-7464-4804-b38a-43e13265c4ac& redirect_uri = http%3A%2F%2F localhost%3A8080%2F'

curl -X POST 
'https://example.com//openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa' 
-H 'Authorization: Basic MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc=' 
-H 'Cache-Control: no-cache' 
-H 'Content-Type: application/x-www-form-urlencoded' 
-d 'code=93317468-7464-4804-b38a-43e13265c4ac&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F'

我无法弄清楚问题。任何人都可以帮助我

I am not able to figure it out the issue . Can anyone please help me

推荐答案

您好Kumar93,

Hi Kumar93,

感谢您发布此处。

根据我的搜索,您可以通过以下两种方式来解决403 Forbidden。

Based on my search, here are two ways for your reference to solve 403 Forbidden.

https://stackoverflow.com/questions/3272067/webclient-403-禁止

https://stackoverflow.com/questions/2794260/webclient-the-remote-server-returned-an-error-403-forbidden

最诚挚的问候,

Wendy


这篇关于403使用Webrequest的禁止错误但在邮递员中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆