我遇到了 Titanium Web 代理的问题。我想要的是在浏览器中加载某些网站之前更改它们的内容。我已经实现了一切,并且工作正常,减去了不断变化的内容部分。这是代码的那一部分:
public async Task OnRequest(object sender, SessionEventArgs e)
{
//Console.WriteLine(e.HttpClient.Request.Url);
// read request headers
var requestHeaders = e.HttpClient.Request.Headers;
var method = e.HttpClient.Request.Method.ToUpper();
if ((method == "POST" || method == "PUT" || method == "PATCH"))
{
// Get/Set request body bytes
byte[] bodyBytes = await e.GetRequestBody();
await Task.Run(() => e.SetRequestBody(bodyBytes));
// Get/Set request body as string
string bodyString = await e.GetRequestBodyAsString();
await Task.Run(() => e.SetRequestBodyString(bodyString));
// store request
// so that you can find it from response handler
e.UserData = e.HttpClient.Request;
}
if(e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("websiteiwanttochange"))
{
byte[] bodyBytes = await e.GetResponseBody();
e.SetRequestBody(bodyBytes);
string bodyString = await e.GetResponseBodyAsString();
e.SetRequestBodyString(bodyString);
e.UserData = e.WebSession.Request;
Console.WriteLine("\n\n\n\n\n\n\nTesting\n\n\n\n\n\n");
Console.WriteLine(bodyBytes.ToString());
//Console.WriteLine(bodyString);
}
// To cancel a request with a custom HTML content
// Filter URL
if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("google.com"))
{
e.Ok("<!DOCTYPE html>" +
"<html><body><h1>" +
"Website Blocked" +
"</h1>" +
"<p>Blocked by titanium web proxy.</p>" +
"</body>" +
"</html>");
}
// Redirect example
if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
{
e.Redirect("https://www.paypal.com");
}
}
这几乎就是我从 Titanium Web Proxy Github 得到的例子。但是,尽管它正确加载了网站,但此代码永远不会到达 ""\n\n\n\n\n\n\nTesting\n\n\n\n\n\n"。
我尝试为该网站添加阻止功能,就像对谷歌一样,它可以正常工作并阻止它。
有谁知道可能是什么问题?我坚持了一段时间,所以任何帮助将不胜感激。
这个例子中没有实现内容改变,我只是想把内容看成字符串,然后尝试改变它。