Calling an API inside an ASP.net MVC Controller Action - asp.net

I am developing an asp.net mvc web application in which I need to call OMDB api (an api to get Imdb information of a movie). I need to send a simple GET request to the api and get the response(movie details), deserialize the response into an object and pass it to the view. Is this possible without using a reference to an external library? Can anybody give me an example on how to do it inside a controller action.

You can use the WebRequest class
using System.Net;
string url = "https://www.service.com?param=movieName";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
Reference:
http://support.microsoft.com/kb/307023

Related

How to add Bearer token to Simple OData Client

New to OData, I need to access SAP Odata Web Service that required Authentication and Token. Say I have the token hardcoded. How to add this token to Simple OData Client?
var settings = new Simple.OData.Client.ODataClientSettings();
settings.BaseUri = new Uri("https://..../UoM?$filter=wer eg '1000' &format=json");
settings.Credentials = new NetworkCredential("user1", "usrpwd");
var client = new ODataClient(settings);
Please kindly help me.
Update --
In this link : Simple Odata Client - How to add oAuth Token in each request header?
It didnot show how to add the hardcoded Token. For my problem, I need to add a given token and make a Odata Request. I check the Odata.org website, I dont seems to find any example for my case.
I have no experience on simple.Odata.client, Can some1 be kind enough to show me how.
Thanks
I believe you can use the ODataClientSettings.BeforeRequest action to alter the request before it is sent.
In the example below I set the Authorization header of the request to 'Bearer <Token>':
settings.BeforeRequest = req => {
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "Your_Token_Here");
};
Of course it is up to you to configure the request for you specific type of authentication.
The URL you use in your example is clearly wrong and not the OData URL for SAP.
You need the base URL for the "yourODataServiceRootURL" below and then add the relative path later in the ODataclient setting eg. "api/data/v9.1"
Instead of using the delegate method to intercept and add the Authorization header on every Http call, a clearer/cleaner solution is to instantiate the ODataClient with an HttpClient instance.
This also allows you to control the HttpClient lifecycle externally.
The code below is an extract of a .Net core app using an Azure AD OAuth2 token to connect to a Dynamics 365 OData Web API.
httpClient.BaseAddress = new Uri(yourODataServiceRootURL);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", yourBearerAccessToken);
//Use the httpClient we setup with the Bearer token header
var odataSettings = new ODataClientSettings(httpClient, new Uri("api/data/v9.1", UriKind.Relative));
var odataClient = new ODataClient(odataSettings);

Xamarin Form not able to invoke the Http URL

I am new to Xamarin Cross-Flatform technology (C#). I am developing one small application where I need to call the http url, get the json data, parse it and display it on the screen.
I am using System.Net.Http for achieving the http call.But request is not reaching to http url
Regards,
Amit Joshi
You can use RestSharp for making http calls.
It is very easy to use.
Code sample:
using RestSharp;
var client = new RestClient ("http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/");
var request = new RestRequest (String.Format ("{0}/allinfo", "198440"));
client.ExecuteAsync (request, response => {
Console.WriteLine (response.Content);
});
RestSharp Examples

How can I call a url from c# code

How can I call a web api url from a csharp console application.
"/api/MemberApi"
I don't need anything back from the server. It just needs to be called and the Web API method will execute some code. Although it would be good to record if the call succeeded.
WebClient class is what you need.
var client = new WebClient();
var content = client.DownloadString("http://example.com");
Example of using WebClient in a console app
MSDN Documentation
You can also use HttpWebRequest if you need to deal with a low level of abstraction but WebClient is a higher-level abstraction built on top of HttpWebRequest to simplify the most common tasks.
Use the HttpWebRequest
HttpWebRequest request = WebRequest.Create("http://www.url.com/api/Memberapi") as HttpWebRequest;
//optional
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream stream = response.GetResponseStream();
Use the response to see if it was successfull or not. There are several Exceptions that can be raised (http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx), which would show you, why your call has failed.

Redirection with a programmatically generated http request in asp.net

I have a web method in second.aspx,which has to be executed only if the incoming request is 'application/json'.So in my First.aspx page I am programmatically generating a Http request with content type set to 'application/json' using the following code.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/website1/Second.aspx");
req.ContentType = "application/json";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
and in the Second.aspx i am checking the incoming request in javascript using <%= Request.ContentType %> to see if it is 'application/json'.If yes I want to execute the web method using jquery ajax method.If I write the streamreader 'sr' to a textbox I can see that <%= Request.ContentType %> gives 'application/json'.But my problem is the HTML of Second.aspx is loaded into the textbox on First.aspx but no redirection to Second.aspx is taking place.so I am unable to exceute the web method this way.
Please could someone help me how do I rediret to Second.aspx page with the above programmatically generated HTTP request code?
You don't. What you are doing is having your application make a web request, read it, and do nothing with it.
Your logic rather convoluted... I don't understand at all why you're doing what you're doing. But if you want to redirect the user to a new page, use Response.Redirect. If you want to execute the page and send the results back to the user use Server.Transfer.

how can i invoke facebook http rest api whitout facebook api lib's (java or C++)

i like to preform simple facebook api call via http rest
but whiteout using facebook java/c++ pre made lib
plain http call
i already done the authorization part and i have the session id and all that .
i just like to see what i need to preform api call over http
thanks
Just use the Facebook Rest API to create your URLs and then send the response via POST
from http://www.exampledepot.com/egs/java.net/Post.html
URL url = new URL("http://hostname:80/cgi");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

Resources