Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I try run code, it throws an error.

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

What I have tried:

My code is

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.sriramachandramedicalcentre.com/api/appserver/app/getAppoimentdetails");
           
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UseDefaultCredentials = true;
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(response.StatusDescription);
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();

Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
Posted
Updated 29-Jul-18 22:21pm

1 solution

You are passing wrong credentials (see HTTP 403 - Wikipedia[^]).

You have to pass credentials for an account on the server instead of the Windows credentials of the user running the application on the client which are used with CredentialCache.DefaultCredentials:
C#
request.Credentials = new System.Net.NetworkCredential(username, password);
// Or with domain:
//request.Credentials = new System.Net.NetworkCredential(username, password, domain);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900