[英]Send json to another server using asp.net core mvc c#
sorry for my english. 对不起我的英语不好。
I need to send json to another server. 我需要将json发送到另一台服务器。
Example: 127.0.0.1/api/start 示例: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 在这台其他服务器上,我正在运行创建的服务,该服务需要获取json
Example {"ServerId":"1","ServerPort":"27015"} 示例{“ 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. 我知道用jquery发送json,但是我想知道如何使用asp.net核心而不使用jquery。 is possible?
有可能吗
public IActionResult Start()
{
// Code here
return View();
}
I need to do this code using asp.net core 我需要使用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);
}
});
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
问问题时不要暴露您的IP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.