Monday, September 8, 2014

Another way to get back the contents of a GET method from an ASP.NET MVC Web API Controller at C#!

This should EVEN work in web forms, but don't expect it to work with POST, PUT, or DELETE.

string url = @"http://www.example.com/api/whatever?foo=bar";
WebRequest webRequest = WebRequest.Create(url);
HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
Stream dataStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(dataStream);
string responseFromServer = streamReader.ReadToEnd();

No comments:

Post a Comment