0

I am trying to consume JSON web service in c# console application. web service is using HTTP basic authentication. I am unable to access in my console application.

Code sample...

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://0000.000.0.000:0000/hrms/rest/login");
request.Method = "POST";
request.ContentType = @"application/json";
//request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("sharads:hrms123"));
HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
3
  • What exception do you get exactly? Commented Oct 14, 2015 at 12:13
  • 1
    stackoverflow.com/questions/8270464/… Commented Oct 14, 2015 at 12:15
  • Have you tried json.net. Makes things a lot easier... Commented Oct 14, 2015 at 12:16

1 Answer 1

0

You can use HttpClient to send HTTP request to JSON Webservice. For example

HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(url);
httpClient.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue(
        "Basic", 
        Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "yourusername", "yourpwd"))));


HttpResponseMessage reponse = httpClient.GetAsync("api/enumproducts/GetAll").Result;
if (reponse.IsSuccessStatusCode)
{
var enumProducts = reponse.Content.ReadAsAsync<List<EnumProduct>>().Result;
}

HttpClient supports also POST Action. For more Details you can take a look at this blog post

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.