Share via


An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code Additional information: The remote server returned an error: (403) Forbidden.

Question

Sunday, May 7, 2017 7:10 AM

I am having problem with my code which shows

"An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code

Additional information: The remote server returned an error: (403) Forbidden."

Please can anyone guide me. Here is my code

StringBuilder postData = new StringBuilder();
            string url = "https://api.hackerearth.com/v3/code/compile/";
            string cl_sec = "CLIENT_SECRET_KEY";   //The key is entered here
            string source = txtcod.Text;
            string lang = "CPP";
            string time_limit = "5";
            string a_sync = "1";
            string memory_limit = "262144";
 
            postData.Append("CLIENT_SECRET=" + HttpUtility.UrlEncode(cl_sec) + "&");
            postData.Append("a_sync=" + HttpUtility.UrlEncode(a_sync) + "&");
            postData.Append("source=" + HttpUtility.UrlEncode(source) + "&");
            postData.Append("Lang=" + HttpUtility.UrlEncode(lang) + "&");
            postData.Append("time_limit=" + HttpUtility.UrlEncode(time_limit) + "&");
            postData.Append("memory_limit=" + HttpUtility.UrlEncode(memory_limit));
 
            StreamWriter writer = null;
 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-WWW-form-encoded-json";
            request.ContentLength = postData.ToString().Length;
            using (writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(postData);
            }
 
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                txtshw.Text = reader.ReadToEnd();
            }

All replies (1)

Sunday, May 7, 2017 4:39 PM âś…Answered

Make sure Client Secret Key is correct, also try to set:

request.Accept= "*/*";
request.UserAgent = "MyApp";