Friday, April 18, 2025

how to use webclient class uploadstring - For webapi, webservice, handler - For post and for get

webclient in asp.net 


for get

 string handlerUrl = String.Format(Request.Url.Scheme+"://" +Request.Url.Authority+"{0}","/api/values");

        string response = (new WebClient()).DownloadString(handlerUrl);


for post

   using (var client = new WebClient())

        {

            string handlerUrl = String.Format(Request.Url.Scheme + "://" + Request.Url.Authority + "{0}", "/api/values");

            client.Headers[HttpRequestHeader.ContentType] =  "application/x-www-form-urlencoded";

            var data = "=Short test...";

            string result = client.UploadString(handlerUrl, "POST", data);

            

            Response.Write(result);

        }


 https://www.aspsnippets.com/Articles/2149/Call-Consume-Web-API-using-WebClient-in-ASPNet-C/

No comments:

Post a Comment