Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

HttpWebRequest C# with controller WebAPI

$
0
0

hi, I am new to asp.net, I encountered a small problem, i create a new webapi controller in MVC project, when i try to get response from this controlle i get  404 error :s 

here is the code of controler used : 

public class SampleController : ApiController
    {
        [HttpPost]
        public string Auth(string number, string password)
        {
            ....
        }

        [HttpPost]
        public string SendSms(string smsTo, string content)
        {
            ....
        }
    }


and here the code c# : 

        static void Main(string[] args)
        {
            var _httpWReq = (HttpWebRequest)WebRequest.Create("http://localhost:64568/api/sample/auth?");

            var encoding = new ASCIIEncoding();
            var data = encoding.GetBytes("number=xxxx&password=xxxx");

            _httpWReq.Method = "POST";
            _httpWReq.ContentType = "application/x-www-form-urlencoded";
            _httpWReq.ContentLength = data.Length;

            using (var stream = _httpWReq.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var x = (HttpWebResponse)_httpWReq.GetResponse(); //the exception :(
            var strResult = new StreamReader(x.GetResponseStream()).ReadToEnd();
        }

the Execption throwed : 
The remote server returned an error: (404) Not Found.


Am I missing something ???


 Thx a lot for your help :).


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>