简体   繁体   中英

403 Forbidden error REST Call

I am building a REST Client in C#. I am getting forbidden 403 error. 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. Please can you help?

This might be related to Env. security profile . For example i had the same issue . Added below in spring security yml file 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.

  • The API that you are calling should whitelist your application IP.
  • Check if there is any http header restrictions on the API that you are calling.
  • For Infra related changes you can check this: link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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