Get HTTP Response from a url by using C#

Lali picture Lali · Apr 3, 2013 · Viewed 7.2k times · Source

Environment: ASP.Net MVC 4 using C#

I need to get image by using GET request to a URL /inbound/faxes/{id}/image I used the code below

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("/inbound/faxes/238991717/image");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

System.IO.StreamReader stream = new StreamReader(response.GetResponseStream());

but it flags "URL not valid"

I used the complete URL www.interfax.net/inbound/faxes/{id}/image

but the result is same

I want to follow this article to receive faxes

Accepting incoming fax notifications by callback

Can anyone help me to get fax...?

Answer

Darin Dimitrov picture Darin Dimitrov · Aug 29, 2013

Try like this:

using (var client = new WebClient())
{
    byte[] imageData = client.DownloadData("http://www.interfax.net/inbound/faxes/{id}/image");
}

Notice how the url is prefixed with the protocol (HTTP in this case). Also make sure you have replaced the {id} part of the url with the actual id of the image you are trying to retrieve.