How to call web API from DotNet 3.5 web application with object?

I developed a web API, I can access my api using HttpClient in .NET 4 and 4.5, but I want to access this api from an existing .NET 3.5 application. Is it possible? I found out from the Internet that HttpClient is not supported in .net 3.5, since I use this service in a .net 3.5 application?

+4
source share
2 answers

Checkout this link for .net version 3.5 :

OR TRY IT FOR 3.5

System.Net.WebClient client = new System.Net.WebClient(); 
client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
 client.BaseAddress = "ENDPOINT URL"; 
  string response = client.DownloadString(string.Format("{0}?{1}", Url, parameters.UrlEncode()));

This is how I call .net 4.5.

var url = "YOUR_URL";
var client = new HttpClient();

var task = client.GetAsync(url);
return task.Result.Content.ReadAsStringAsync().Result;
+5
source

You can use webrequest

 // Create a request for the URL.       
 var request = WebRequest.Create ("http://www.contoso.com/default.html");
 request.ContentType = contentType; //your contentType, Json, text,etc. -- or comment, for text
 request.Method = method; //method, GET, POST, etc -- or comment for GET
 using(WebResponse resp = request.GetResponse())
 {
    if(resp == null)
        new Exception("Response is null");

    return resp.GetResponseStream();//Get stream
}
+3

Source: https://habr.com/ru/post/1568616/


All Articles