Titanium.Web.Proxy what am I doing wrong?

Need for the project to use your proxy server, the choice settled on Titanium.Web.Proxy. In General everything works fine except the point where you want to change the response from the server as soon as you try to do you get the browser - Error code 324 (net::ERR_EMPTY_RESPONSE). Decided to take a cue from gichaba and just try to change the page title. Was this code:

static void Main(string[] args)
{
 var proxyServer = new ProxyServer();
 proxyServer.TrustRootCertificate = true;

 proxyServer.BeforeRequest += OnRequest;
 proxyServer.BeforeResponse += OnResponse;
 proxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
 proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;

 var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true)
{
 //Exclude Https addresses you don't want to proxy
 //Usefull for clients that use certificate pinning
 //for example dropbox.com
 // ExcludedHttpsHostNameRegex = new List<string>() { "google.com", "dropbox.com" }

 //Use self-issued certificate generic on all https requests
 //Optimizes performance by not creating a certificate for each https-enabled domain
 //Usefull when certificate trust is not requiered by proxy clients
 // GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")
};

proxyServer.AddEndPoint(explicitEndPoint);
proxyServer.Start();

 foreach (var endPoint in proxyServer.ProxyEndPoints)
 Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
 endPoint.GetType().Name of endPoint.IpAddress endPoint.Port);

Console.Read();

 //Unsubscribe & Quit
 proxyServer.BeforeRequest -= OnRequest;
 proxyServer.BeforeResponse -= OnResponse;
 proxyServer.ServerCertificateValidationCallback -= OnCertificateValidation;
 proxyServer.ClientCertificateSelectionCallback -= OnCertificateSelection;
proxyServer.Stop();
}

 private static async Task OnRequest(object sender, SessionEventArgs e)
{
Console.WriteLine(e.WebSession.Request.Url);

 ////read request headers
 var requestHeaders = e.WebSession.Request.RequestHeaders;

 var method = e.WebSession.Request.Method.ToUpper();
 if ((method == "POST" || method == "PUT" || method == "PATCH"))
{
 //Get/Set request body bytes
 byte[] bodyBytes = await e.GetRequestBody();
 await e.SetRequestBody(bodyBytes);

 //Get/Set request body as string
 string bodyString = await e.GetRequestBodyAsString();
 await e.SetRequestBodyString(bodyString);

}

 //To cancel a request with a custom HTML content
 //Filter URL
 if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
{
 await e.Ok("" +
 "<h1>" +
 "Website Blocked" +
 "</h1>" +
 "<p>Blocked by titanium web proxy.</p>" +
 "" +
"");
}
 //Redirect example
 if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
{
 await e.Redirect("https://www.paypal.com");
}
}

 private static async Task OnResponse(object sender, SessionEventArgs e)
{

 //read response headers
 var responseHeaders = e.WebSession.Response.ResponseHeaders;
//e.WebSession.Response.ResponseHeaders.Remove("Content-Security-Policy");

 //if (!e.ProxySession.Request.Host.Equals("medeczane.sgk.gov.tr")) return;
 if (e.WebSession.Request.Method == "GET" || e.WebSession.Request.Method == "POST")
{
 if (e.WebSession.Response.ResponseStatusCode == "200")
{
 if (e.WebSession.Response.ContentType != null && e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html"))
{
 byte[] bodyBytes = await e.GetResponseBody();
 await e.SetResponseBody(bodyBytes);

 string body = await e.GetResponseBodyAsString();
 body = body.Replace("<title>", "<title>xxxxxxxxxxxxxxxxxxxxxxxxxx");

 await e.SetResponseBodyString(body);
}
}
}
}
 /// Allows overriding default certificate validation logic
 private static Task OnCertificateValidation(object sender, CertificateValidationEventArgs e)
{
 //set IsValid to true/false based on Certificate Errors
 if (e.SslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
 e.IsValid = true;

 return Task.FromResult(0);
}

 /// Allows overriding default client certificate selection logic during mutual authentication
 private static Task OnCertificateSelection(object sender, CertificateSelectionEventArgs e)
{
 //set e.clientCertificate to override
 return Task.FromResult(0);
}</code></pre><br><br>
But... again in my browser - Error code 324 (net::ERR_EMPTY_RESPONSE)<br>
If you comment out the line <code class="cs">proxyServer.BeforeResponse += OnResponse;</code> then it works, by trial it was found that if the <pre><code class="cs">private static async Task OnResponse(object sender, SessionEventArgs e)</code></pre> leave only one row of <code class="cs">string body = await e.GetResponseBodyAsString();</code> or <code class="cs">byte[] bodyBytes = await e.GetResponseBody();</code> then all stops working. What am I doing wrong? After all, take an example from the project page.</title></string>
June 27th 19 at 15:18
1 answer
June 27th 19 at 15:20
Solution
The answer is found used net 4.5, after the transition to net 4.5.2 the problem disappeared...

Find more questions by tags C#