protected void Page_Load(object sender, EventArgs e) { try { bool debug = false; var proxyData = new ProxyOptions(); if (proxyData.Ready) { HttpWebRequest proxyRequest = null; // Create request stub for get or post switch (proxyData.Method) { case "POST": case "PUT": case "PATCH": case "MERGE": proxyRequest = CreatePostRequest(proxyData); break; default: proxyRequest = CreateGetRequest(proxyData); break; } // Shared request properties if (proxyRequest != null) { proxyRequest.Method = proxyData.Method; proxyRequest.ContentType = proxyData.ContentType; proxyRequest.Accept = proxyData.Accept; this.CopyHeaders(proxyRequest, proxyData.OriginalRequest); ExecuteAndWriteProxyResponse(proxyRequest); } } else { throw new Exception("There was a problem parsing the proxy request options provided."); } } catch (ThreadAbortException) { // response.end generates this } catch (Exception ex) { // Writing a plain 500 trashes all custom messages Response.ClearHeaders(); Response.ClearContent(); Response.Status = "503 ServiceUnavailable"; Response.StatusCode = 503; Response.StatusDescription = ex.Message; // writes to header Response.Flush(); throw new HttpException(503, ex.Message); // writes to body } }
private HttpWebRequest CreatePostRequest(ProxyOptions proxyData) { var request = (HttpWebRequest)WebRequest.Create(proxyData.Url); request.Method = proxyData.Method; // must set this to be able to write body Stream reqStream = request.GetRequestStream(); byte[] reqBytes = Encoding.UTF8.GetBytes(proxyData.Data); reqStream.Write(reqBytes, 0, reqBytes.Length); reqStream.Close(); return request; }
private HttpWebRequest CreateGetRequest(ProxyOptions proxyData) { return (HttpWebRequest)WebRequest.Create(string.Format("{0}?{1}", proxyData.Url, proxyData.Data)); }