2

I would like t send data from one of my asp.net application to another. I'm trying to do it using HttpClient.

My First application:

public class PostVacanciesController : Controller
{
    public myEntity db = new myEntity();
    public const string sendAppURL = "http://localhost:51394/SecondApp/sendData";

    public ActionResult PostTest()
    {
        try
        {
            PostData dataPost = new PostData();

            // Some code to get PostData from database

            var myContent = JsonConvert.SerializeObject(dataPost);
            var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
            var byteContent = new ByteArrayContent(buffer);
            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(sendAppURL);
            var result = client.PostAsync("", byteContent).Result;

            return new HttpStatusCodeResult(HttpStatusCode.OK);
        } catch(Exception any)
        {
            return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
        }
    }
}

My PostData model:

public class PostData
{
    public int PropertyID { get; set; }
    public List<int> Units { get; set; } = null;
}

Here is my second application here I try to get the data

public class SecondAppController : Controller
{

    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [System.Web.Http.HttpPost]
    public ActionResult sendData(FeedData receiveData)
    { 
        // receiveData variable is wrong.
        // It is showing PropertyID = 0 and Units = null

        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }
}

My FeedData model:

public class FeedData
{
    public int PropertyID { get; set; }
    public List<int> Units { get; set; } = null;
}

Does anyone know why it is not sending the data? If I create a break point before calling the second App, I can see that it has data to send.

Thanks

4
  • Side note, why do you initialize your List<int> to null? That's redundant because null is the default value for all objects. Commented Apr 25, 2018 at 15:42
  • Why using ByteArrayContent and not StringContent? Commented Apr 25, 2018 at 15:55
  • Guys... I start started using asp.net, so my code might be not correct. However, I am not getting any error, just the data is not being sent Commented Apr 25, 2018 at 16:02
  • @MaxBoy confirm which version of asp.net mvc you are using for both projects Commented Apr 25, 2018 at 16:07

1 Answer 1

1

Update action to use StringContent as you are already serializing the JSON string.

public const string sendAppURL = "http://localhost:51394/SecondApp/sendData";
static HttpClient client = new HttpClient() {
    BaseAddress = new Uri(sendAppURL)
};

public async Task<ActionResult> PostTest() {
    try {
        PostData dataPost = new PostData();

        // Some code to get PostData from database

        var json = JsonConvert.SerializeObject(dataPost);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var response = await client.PostAsync("", content);

        return new HttpStatusCodeResult(response.StatusCode);
    } catch(Exception any) {
        return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
    }
}

Note the inclusion of the async syntax.

You need to verify which HttpPost attribute you are using as the second controller has [System.Web.Http.HttpPost] while it inherits from System.Web.Mvc.Controller which uses the [System.Web.Mvc.HttpPost]

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.