Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Call Web API From DotNet 3.5 Web Application with Object?

I have developed a Web API, I can access my api by 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 have learned from internet that HttpClient is not supported in .net 3.5, so how I consume this service in .net 3.5 application?

like image 595
Pritam Jyoti Ray Avatar asked Dec 22 '14 12:12

Pritam Jyoti Ray


2 Answers

Checkout this link for .net version 3.5:

OR TRY THIS 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 am making call in .net 4.5.

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

var task = client.GetAsync(url);
return task.Result.Content.ReadAsStringAsync().Result;
like image 111
SharpCoder Avatar answered Sep 18 '22 06:09

SharpCoder


You could 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
}
like image 37
pikax Avatar answered Sep 21 '22 06:09

pikax