简体   繁体   English

在 C# 中接收 Web 服务中的请求?

[英]receive requests in web servce rest in c #?

I have a web service api rest project in C # (basic) in VS, I program a POST method that does nothing at the moment, it only validates if information arrives or that I think it does ha, what I want to know is: when I send a Json chain from a client program, to be able to debug the reception of the request sent from my web service.我在 VS 的 C#(基本)中有一个 Web 服务 api rest 项目,我编写了一个 POST 方法,目前什么都不做,它只验证信息是否到达或我认为它确实存在,我想知道的是:当我从客户端程序发送 Json 链时,能够调试从我的 Web 服务发送的请求的接收。 I want to see how my request arrives.我想看看我的请求是如何到达的。

The web service is made with the templates that come as an example in Visual Studio, which says web API fullRest, add the controllers folder where the POST and GET methods of my web service are located. Web 服务是使用 Visual Studio 中作为示例的模板制作的,它表示 Web API fullRest,在我的 Web 服务的 POST 和 GET 方法所在的位置添加控制器文件夹。 Basically my project is very basic, everything is programmed by the template, I just added the methods.基本上我的项目非常基础,一切都是由模板编程的,我只是添加了方法。

here is my code for the POST method in my web service rest:这是我的 Web 服务休息中 POST 方法的代码:

[Route("api/prueba")]
    [HttpPost]
    public HttpResponseMessage Post([FromBody] datosTarjeta datoTarjeta )
    {
        try
        {
            if (datoTarjeta != null)
            {
                var respuesta = new HttpResponseMessage(HttpStatusCode.OK);

                return respuesta;
            }
        }
        catch (Exception ex)
        {
            var respuesta = new HttpResponseMessage(HttpStatusCode.BadRequest);
            return respuesta;
        }
        var respuesta1 = new HttpResponseMessage(HttpStatusCode.BadRequest);
        return respuesta1;

    }

}

This is the code that sent the Json string to the web service, it is in VB.net, which sends a Json string:这是将 Json 字符串发送到 Web 服务的代码,它在 VB.net 中,它发送一个 Json 字符串:

Imports System
Imports System.Net
Imports System.Text
Imports System.Threading
Imports Newtonsoft.Json

Public Async Sub metodoPost()
    Dim t As New datosTarjeta
    t.Ln_Tarj = "PRO1"
    t.Fiid_Tarj = "B062"
    t.Numero_de_tarjeta = "5359430105739184   "

    Try
        Dim url As String

        url = "http://localhost:60973/api/rest"

        Dim cliente As New Http.HttpClient
        Dim JsonData As String = JsonConvert.SerializeObject(t)
        Dim restContenido As New Http.StringContent(JsonData, Encoding.UTF8, "application/json")
        Dim restRespuesta As Http.HttpResponseMessage = Await cliente.PostAsync(url, restContenido)
        Thread.Sleep(200000)
        Console.WriteLine(restRespuesta.StatusCode.ToString)

    Catch e As Exception
        Dim mensaje As String

        mensaje = e.Message

    End Try


End Sub

If you could help me to know at what point the request reaches the web service or how I can program a method for the paticion to arrive at the web service and be able to see how the request arrives.如果您能帮助我知道请求到达 Web 服务的时间点,或者我如何编写一种方法让 paticion 到达 Web 服务并能够看到请求是如何到达的。 Thank you.谢谢你。

That request probably won't arrive, because you told the server the route is api/prueba :该请求可能不会到达,因为您告诉服务器路由是api/prueba

[Route("api/prueba")]
[HttpPost]
public HttpResponseMessage Post([FromBody] datosTarjeta datoTarjeta )

And in the client you posted to api/rest在您发布到api/rest的客户端中

url = "http://localhost:60973/api/rest"

First off, I recommend that you don't try to write the client and the server.首先,我建议您不要尝试编写客户端和服务器。 Just write the server, and use Postman to poke it with data, check it works.. then write the client.只需编写服务器,然后使用 Postman 用数据戳它,检查它是否有效..然后编写客户端。 Battling your own client bugs as well as trying to get its config right is a headache you don't need.与您自己的客户端错误作斗争以及尝试使其配置正确是您不需要的头痛。 Postman is reliable and a good tool for sending JSON to RESTful webserviecs like you have here. Postman 是可靠的,是将 JSON 发送到 RESTful webservices 的好工具,就像你在这里看到的一样。 Set a breakpoint in the top of the Post method (call it something else, like.. what it does: UpdateUser, CreateOrder, ChangeShippingAddress etc) then put the url and data into postman and hit Go;在 Post 方法的顶部设置一个断点(将其称为其他名称,例如..它的作用:UpdateUser、CreateOrder、ChangeShippingAddress 等)然后将 url 和数据放入 postman 并点击 Go; your server code should break and you can step through it您的服务器代码应该会中断,您可以单步执行

Then you can write your client;然后你就可以写你的客户端了; put the same url and data in, hit Go..输入相同的 url 和数据,点击 Go..


You could probably make your life somewhat easier by adding something like Swashbuckle, Swagger-Net or NSwag package to your server, and then using the Add REST Client feature in VS, or the AutoRest tool (or, apparently NSwag generates client side code too)..您可能可以通过向服务器添加诸如 Swashbuckle、Swagger-Net 或 NSwag 包之类的东西,然后使用 VS 中的Add REST Client功能或 AutoRest 工具(或者,显然 NSwag 也生成客户端代码)使您的生活更轻松..

All this "creating a client by crafting an HTTP request by hand" - boring, repetitive etc;所有这些“通过手工制作 HTTP 请求来创建客户端”——无聊、重复等; you didn't "receive and parse the HTTP request by hand" on the server end, so why not have the technology write the client side for you too;您没有在服务器端“手动接收和解析 HTTP 请求”,那么为什么不让该技术也为您编写客户端; swagger describes the service in a standard way, that means another tool can consume the description and write client side code that has nicely named methods etc swagger 以标准方式描述服务,这意味着另一个工具可以使用描述并编写具有很好命名的方法等的客户端代码

It's like the days of old with WCF/ASMX/SOAP/XML/"Add Service Reference" (except in those days it was easier and more reliable)就像过去使用 WCF/ASMX/SOAP/XML/“添加服务引用”一样(除了在那些日子里它更容易和更可靠)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM