2017-02-26 87 views
0

对不起,我的英语JSON发送到另一台服务器。使用asp.net mvc的核心C#

我需要JSON发送到另一台服务器。

例子:127.0.0.1/api/start

在这个其他的服务器我有我创建运行服务,服务需要得到一个JSON

示例{“SERVERID”:“1” “ServerPort”: “27015”}

我怎样才能做到这一点?

我知道用jQuery发送JSON,但我想知道如何不使用jQuery做到这一点使用asp.net核心。有可能吗?

public IActionResult Start() 
    { 

     // Code here 

     return View(); 
    } 

我需要使用asp.net核心做这个代码

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); 
       } 
      }); 

回答

0

你必须改变这样的

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(); 
    } 

,并在这里你的方法什么响应

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 
} 

所以你的服务器没有运行或你的代理做的请求:)过滤器

当你问一个问题

+0

HM ..但IP,我需要发送JSON是13.77.102.81,不要暴露你的IP这里我具体该? –

+0

更准确的说是要送13.77.102.81/api/mta/start –

+0

或您的服务发送GET请求?有两件事 –