403禁止错误REST调用

时间:2015-04-06 12:56:48

标签: c# rest

我正在用C#构建一个REST客户端。我被禁止403错误。这是我的代码

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);
        }

    }
}

}

我还提供了凭据以及启用了默认凭据。我仍然得到403错误。 你能帮忙吗?

2 个答案:

答案 0 :(得分:0)

这可能与Env有关。安全配置文件。例如我有同样的问题。在 spring security yml 文件 securityCollections: - name: "Security" patterns: - "/rootpath/*"

下面添加

答案 1 :(得分:0)

Http Forbidden 错误通常发生在您尝试调用服务器而服务器不允许请求时。

  • 您正在调用的 API 应将您的应用 IP 列入白名单。
  • 检查您调用的 API 是否存在任何 http 标头限制。
  • 对于 Infra 相关的更改,您可以查看:link