none
how to call web api in asp.net c# RRS feed

  • Question

  • string mob = "test";
            HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("https://test./api/GetClientID.pdo");
            Encoding encoding = new UTF8Encoding();
            string postData = "{\"clientName\":[\"" + mob + "\"]}";
            byte[] data = encoding.GetBytes(postData);

            httpWReq.ProtocolVersion = HttpVersion.Version11;
            httpWReq.Method = "POST";
            httpWReq.ContentType = "application/json";
            string _auth = string.Format("{0}:{1}", "327c1ab8-2cfss4-11e8-94c3-6305cb1154111111bb1b3","3f0eed38-2cf4-11e8-a9bsfrfra-d386efrggrgge528908541");
            string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));
            string _cred = string.Format("{0} {1}", "Basic", _enc);
            httpWReq.Headers[HttpRequestHeader.Authorization] = _cred;
            httpWReq.ContentLength = data.Length;


            Stream stream = httpWReq.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();

            HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
            string s = response.ToString();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            String jsonresponse = "";
            String temp = null;
            while ((temp = reader.ReadLine()) != null)
            {
                jsonresponse += temp;
            }  

    i am getting below error.

    Plz help me on this

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

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.Net.WebException: The remote server returned an error: (404) Not Found.

    Source Error: 
    Line 37:         stream.Close();
    Line 38: 
    Line 39:         HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
    Line 40:         string s = response.ToString();
    Line 41:         StreamReader reader = new StreamReader(response.GetResponseStream());

    Tuesday, March 27, 2018 9:40 AM

All replies

  • Something is wrong with the URL, and the site cannot be found,  a 404 error.

    !) you can use Fiddler (free) to look at the raw URL at the time of the 404.

    2) WebAPI can be discussed at the WebAPI forum in ASP.NET forums.

    http://forums.asp.net/

    Tuesday, March 27, 2018 2:52 PM
  • Hi Vicky,

    What is definition of web api?

    As the suggestion from DA924x, there is something wrong with your address, maybe it did not exist.

    I would suggest you try PostMan to send your request to web api first.

    Best Regards,

    Tao Zhou


    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.

    Wednesday, March 28, 2018 6:22 AM