0

I want to call the JAVA Restful webservice POST (want to send JSON array) from C# console program.

This is the JAVA Restful webservice

This is the page when I test webservice.

This is a piece of C# codes that will send JSON array to java restful webservice.

It did not write text file in JAVA webservice and I think my C# codes went something wrong while calling to webservice. My JSON array = sb.toString().

Please help me. Thank you.

Silvia
  • 95
  • 1
  • 2
  • 9

1 Answers1

0

I don't want to distract you from what you doing but here is a stack thread that doing something like your work How do I make calls to a REST api using c#? and as you can see if your rest api only accepts json you should set that

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;

namespace ConsoleProgram
{
    public class DataObject
    {
        public string Name { get; set; }
    }

    public class Class1
    {
        private const string URL = "https://sub.domain.com/objects.json";
        private string urlParameters = "?api_key=123";

        static void Main(string[] args)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(URL);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            // List data response.
            HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call!
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result;
                foreach (var d in dataObjects)
                {
                    Console.WriteLine("{0}", d.Name);
                }
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }  
        }
    }
}
Community
  • 1
  • 1
sam stone
  • 703
  • 5
  • 16