简体   繁体   English

使用ASP.NET Core MVC C#将JSON发送到另一台服务器

[英]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.

相关问题 从 ASP.NET MVC 项目后面的 C# 代码向另一个 web 站点发送 JSON 请求 - Send a JSON request to another web site from C# code behind in an ASP.NET MVC project 使用 C# 从 ASP.NET Core MVC 中的 URL 解析 JSON 数据不起作用 - Parse JSON data from URL in ASP.NET Core MVC using C# doesn't work 使用 C# 反序列化 API Json 时出错 - Z9E0DA8438E1E38A1C30F4B76CE7 Core - Error in Deserializing the API Json using C# - ASP.NET Core MVC 将 blob 从一个 Azure 容器复制到另一个使用 ASP.NET Core 6.0 MVC 和 C# - Copy blobs from one Azure container to another using ASP.NET Core 6.0 MVC with C# 如何使用服务器ASP.NET MVC C#发送“谷歌分析”和“ hotjar”跟踪客户端事件 - How to send “google analytics” and “hotjar” tracking client events using server ASP.NET MVC C# 如何使用 C# 在 ASP.NET 核心 MVC 中将 model 从一个动作传递到另一个动作而不使用 TempData - How to pass model from one action to another without using TempData in ASP.NET Core MVC using C# 如何向 C# 中的 ASP.NET Core MVC controller 发送数据? - How to send data to the ASP.NET Core MVC controller in C#? 如何在ASP.Net Core MVC模型中使用C#从rest api解析json数据并将其显示在HTML页面上 - how to parse json data from a rest api using C# in the ASP.Net Core MVC model and display it on a HTML page 如何使用C#ASP.NET HttpClient将数据从Sql Server发送到另一个Sql Server(未链接) - How to send data from a Sql Server to another Sql Server (Not Linked) Using C# ASP.NET HttpClient ASP.NET Core Web API如何从客户端C#发送json对象 - asp.net core web api how to send json object from client side c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM