none
How can i send parameter by web service rest RRS feed

  • Question

  • I need to send a parameter by web service from my App Android. I must to use http verbs as post? For example:

    [AcceptVerbs("Post")]
    [Route("{value}")]

    This parameter other program will consume it. How can i do this?

    I use web api with restfull.

    I did this way. The code in my App:

    public async Task PushToken(string value)
            {
                try
                {
                    string url = $"http://www.inetglobal.com.br/autorizador/api/gettoken/{value}";
                    var uri = new Uri(string.Format(url));
                    var data = JsonConvert.SerializeObject(value);
                    var content = new StringContent(data, Encoding.UTF8, "application/json");
                    HttpResponseMessage response = null;
                    response = await client.PostAsync(uri, content);
    
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new Exception("Erro ao tentar enviar o token");
                    }
                }
                catch(Exception ex)
                {
                }
            }

    My service(Controller)

    public class GetTokenController : ApiController
        {
            GetTokenAutorizador getToken = new GetTokenAutorizador();
    
            [Route("{value}")]
            [AcceptVerbs("Post")]
            public string getTokenApp(string value)
            {
                return getToken.getTokenApp(value);
            }
        }

    and my Model:

    public class GetTokenAutorizador
        {
            [Route("{value}")]
            public string getTokenApp(string value)
            {
                return value;
            }
        }

    The code above i still didn't try out, due some technical issue now.
    • Edited by pnet Tuesday, October 3, 2017 5:12 PM
    • Moved by Sabah ShariqMVP Tuesday, October 3, 2017 8:39 PM Moved From Visual C#
    Tuesday, October 3, 2017 4:25 PM

All replies

  • I use web api with restfull

    https://forums.asp.net/1246.aspx/1?Web+API

    it's where you should post.

    Wednesday, October 4, 2017 8:04 PM
  • Hi pnet,

    I suggest you make a test with below steps.

    1. Web APi

    namespace WCFWebApp
    {
        [RoutePrefix("api/GetToken")]
        public class GetTokenController : ApiController
        {
            GetTokenAutorizador getToken = new GetTokenAutorizador();
            [Route("getTokenApp")]
            [HttpPost]
            public string getTokenApp([FromBody]string value)
            {
                return getToken.getTokenApp(value);
            }
            // GET api/<controller>
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET api/<controller>/5
            public string Get(int id)
            {
                return "value";
            }
    
            // POST api/<controller>
            public void Post([FromBody]string value)
            {
            }
    
            // PUT api/<controller>/5
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE api/<controller>/5
            public void Delete(int id)
            {
            }
        }
        public class GetTokenAutorizador
        {
            [Route("{value}")]
            public string getTokenApp(string value)
            {
                return value;
            }
        }
    }

    2. Client code

    try
                {
                    HttpClient client = new HttpClient();
                    string value = "Test";
                    string url = $"http://localhost:19023/api/gettoken/getTokenApp";
                    var uri = new Uri(string.Format(url));
                    var data = JsonConvert.SerializeObject(value);
                    var content = new StringContent(data, Encoding.UTF8, "application/json");
                    HttpResponseMessage response = null;
                    response = client.PostAsync(uri, content).Result;
    
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new Exception("Erro ao tentar enviar o token");
                    }
                }
                catch (Exception ex)
                {
                }

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 5, 2017 6:59 AM
  • Edward, i guess that my service is wrong. I don't need post service but get service. This is my business. When my App start it generates a token. I need to send this token to other application. Then, my service get this token and hand over this token for other application. Then i guess that my service is GET and not POST, all right?
    Thursday, October 5, 2017 11:54 AM
  • Hi pnet,

    >> I don't need post service but get service

    Do you mean you want to use Get Http request instead of Post Http?

    >> my service get this token and hand over this token for other application

    In general, if you want to send something from client to service, Post would be better. If you fill your value in Http Get Url, it will be visible by others who are able to capture request between client and services.

    In my option, both Get and Post could achieve your requirement.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 6, 2017 7:04 AM