C# 使用asp.net核心mvc将json发送到另一台服务器#
对不起我的英语 我需要将json发送到另一台服务器 示例:127.0.0.1/api/start 在另一台服务器中,我创建的服务正在运行,该服务需要获取json 示例{“ServerId”:“1”,“ServerPort”:“27015”} 我该怎么做 我知道使用jquery发送json,但我想知道如何使用asp.net内核而不使用jquery。有可能吗C# 使用asp.net核心mvc将json发送到另一台服务器#,c#,json,asp.net-core,asp.net-core-mvc,C#,Json,Asp.net Core,Asp.net Core Mvc,对不起我的英语 我需要将json发送到另一台服务器 示例:127.0.0.1/api/start 在另一台服务器中,我创建的服务正在运行,该服务需要获取json 示例{“ServerId”:“1”,“ServerPort”:“27015”} 我该怎么做 我知道使用jquery发送json,但我想知道如何使用asp.net内核而不使用jquery。有可能吗 public IActionResult Start() { // Code here re
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);
}
});
你必须像这样改变你的方法
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
}
因此,您的服务器未运行,或者您的代理对请求进行筛选:)
提问时不要暴露您的IP
陛下但是我需要发送json的ip是13.77.102.81,我在这里指定了它?更确切地说是13.77.102.81/api/mta/startyou想要发送还是您的服务发送get请求?有两件事我正在将服务器A上的json发送到服务器B。我想将json发送到另一台服务器。