mardi 27 mars 2018

how to call web api in asp.net c#

protected void btnsave_Click(object sender, EventArgs e) { 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}", "3327sc1ab8-2c4f4-1156e8-94c3-6305cvffb1bb1b3","38f0edfdf38-2cf4-11rrge8-a9ba-d386Rvfve5208541");
    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());




Aucun commentaire:

Enregistrer un commentaire