Link to home
Start Free TrialLog in
Avatar of ukerandi
ukerandiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

C# webservices

Hi,
I'm getting following Error Messages

+            response      {StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Access-Control-Allow-Origin: *
  Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
  Access-Control-Allow-Headers: Authorization,Content-Type,X-Requested-With, Accept
  Cache-Control: no-cache
  Date: Mon, 06 Nov 2017 23:18:17 GMT
  Server: Microsoft-IIS/8.5
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 2388
  Content-Type: application/json; charset=utf-8
}}      System.Net.Http.HttpResponseMessage


My code
using (var client = new HttpClient())
            {
                client.BaseAddress = this.baseUri;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                    "Bearer",
                    this.userObject.access_token);

                var job = new Job
                {
                    DestinationAddress = string.Empty,
                    DestinationDescription = "abc",
                    DestinationName = "dsdsad",
                    VehicleId = 0,
                    DestinationLatitude = latitude,
                    DestinationLongitude = longitude,
                    Message = "Test ting"
                };

                var content = new JavaScriptSerializer().Serialize(job);
          
                var response = await client.PostAsync(
                                   $"/public/{Version}/jobs",
                                   new StringContent(content, Encoding.UTF8, "application/json"));

Open in new window

Avatar of Mayur Agarwal
Mayur Agarwal

try below code.Kindly change the service URL address as per your application.Don't forget to run your service.
string serviceUrl = "http://localhost/WebApiDemo/api/Employees/AddEmployee";
 client.BaseAddress = new Uri(serviceUrl);
lient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

Open in new window

instead of
client.BaseAddress = this.baseUri;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.