Skip to content

Instantly share code, notes, and snippets.

@tyconsulting
Last active October 13, 2016 10:55
Show Gist options
  • Save tyconsulting/eae44357f14818006bf0ba94bf07bae1 to your computer and use it in GitHub Desktop.
Save tyconsulting/eae44357f14818006bf0ba94bf07bae1 to your computer and use it in GitHub Desktop.
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.IO;
using System.Text;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger proxy function processed a request. RequestUri={req.RequestUri}");
// parse query parameter
string requesturl = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "requesturl", true) == 0)
.Value;
log.Info($"C# HTTP trigger proxy function is trying to get response from {requesturl}");
Encoding outputencoding = Encoding.GetEncoding("ASCII");
var ProxyResponse = new HttpResponseMessage();
HttpStatusCode statuscode = new HttpStatusCode();
if (requesturl == null)
{
statuscode = HttpStatusCode.BadRequest;
StringContent errorcontent =new StringContent("Please pass a web request url on the query string or in the request body", outputencoding);
ProxyResponse = req.CreateResponse(statuscode, errorcontent);
ProxyResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
ProxyResponse.Content = errorcontent;
} else {
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(requesturl);
ProxyResponse = response;
ProxyResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
}
return ProxyResponse;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment