0

sorry for my english.

I need to send json to another server.

Example: 127.0.0.1/api/start

In this other server i have the service that i created running, the service need to get a json

Example {"ServerId":"1","ServerPort":"27015"}

How can i do this?

I know send json with jquery, but i want know how do this using asp.net core without use jquery. is possible?

    public IActionResult Start()
    {

       // Code here

        return View();
    }

I need to do this code using asp.net core

var arr = { ServerId : '1', ServerPort : '27015'};
            console.log(JSON.stringify(arr));            
            return jQuery.ajax({
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                'type': 'POST',
                'url': '13.77.102.81/Api/MTA/Start',
                'data': JSON.stringify(arr),
                'dataType': 'json',
                'success': function (msg) {
                    console.log(msg);
                }
            });

1 Answers1

0

you have to change your method something like this

public IActionResult Start()
    {


     var jsonRequest = Json(new { ServerId = "1", ServerPort = "27015" }).Value.ToString();
                    HttpClient client = new HttpClient();
                    client.BaseAddress = new Uri("http://13.77.102.81/api/mta/start ");
                    client.DefaultRequestHeaders
                          .Accept
                          .Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header

                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");

                    request.Content = new StringContent(jsonRequest,
                                                        Encoding.UTF8,
                                                        "application/json");//CONTENT-TYPE header

                    client.SendAsync(request)
                          .ContinueWith(responseTask =>
                          {
                               //here your response 
                              Debug.WriteLine("Response: {0}", responseTask.Result);
                          });
        return View();
    }

and here the response

Response: StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Connection: keep-alive
  Date: Sun, 26 Feb 2017 21:43:26 GMT
  Server: nginx/1.4.6
  Server: (Ubuntu)
  Content-Length: 0
}

so your server is not running or your proxy does filters on requests :)

Do not expose your IP when you ask a question

BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47