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 Answer 1

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

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

10 Comments

hm.. but the ip that i need send json is 13.77.102.81, where i specific this?
to be more exact is 13.77.102.81/api/mta/start
you want to send or your service send a get request ? there are 2 things
i'm sending the json on server A to server B.
i want to send the json to another server.
|

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.