简体   繁体   English

403 禁止错误 REST 调用

[英]403 Forbidden error REST Call

I am building a REST Client in C#.我正在用 C# 构建一个 REST 客户端。 I am getting forbidden 403 error.我收到了被禁止的 403 错误。 Here is my code这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using restservice.webref;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;


namespace restservice
{
    class getUUID
    {
        private const string URL = "http://XXX.XXXX.com/ws410/XXXXX/XX/XX";
        public void getProductID()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "GET";

        request.Credentials = new NetworkCredential("XXXX", "XX");
        request.Accept = @"text/html, application/xhtml+xml, */*";
        request.Referer = @"http://www.somesite.com/";
        request.Headers.Add("Accept-Language", "en-GB");
        request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";
        request.Host = @"www.my.XXX.com";
        request.UseDefaultCredentials = true;
        request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
        request.ContentType = "application/json";
        /*StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
        requestWriter.Write(false);
        requestWriter.Close();*/
        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                String htmlString = reader.ReadToEnd();
            }
            /*
            WebResponse webResponse = request.GetResponse();
            Stream webStream = webResponse.GetResponseStream();
            StreamReader responseReader = new StreamReader(webStream);
            string response = responseReader.ReadToEnd();
            Console.Out.WriteLine(response);
            responseReader.Close();*/
        }
        catch (Exception e)
        {
            Console.Out.WriteLine(e.Message);
        }

    }
}

} }

I have also given the credentials as well as enabled the default credentials.我还提供了凭据并启用了默认凭据。 Still I get the 403 error.我仍然收到 403 错误。 Please can you help?请问你能帮忙吗?

This might be related to Env.这可能与Env有关。 security profile .安全配置文件。 For example i had the same issue .例如我有同样的问题。 Added below in spring security yml file securityCollections: - name: "Security" patterns: - "/rootpath/*"在 spring security yml 文件securityCollections: - name: "Security" patterns: - "/rootpath/*"添加以下内容securityCollections: - name: "Security" patterns: - "/rootpath/*"

Http Forbidden error usually occurs when you are trying to call the server and server is not allowing the request.当您尝试调用服务器而服务器不允许请求时,通常会发生 Http Forbidden 错误。

  • The API that you are calling should whitelist your application IP.您正在调用的 API 应将您的应用程序 IP 列入白名单。
  • Check if there is any http header restrictions on the API that you are calling.检查您正在调用的 API 是否存在任何 http 标头限制。
  • For Infra related changes you can check this: link对于 Infra 相关的更改,您可以检查这个: 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM