C# HttpWebResponse是否发送到web浏览器?

C# HttpWebResponse是否发送到web浏览器?,c#,C#,我是c#新手,正在制作一个简单的代理来编辑某些标题 我使用HttpLisenter获取请求,然后使用HttpWebRequest和Response进行编辑、发送和接收 现在,我需要一些帮助来将编辑后的响应发送回web浏览器。有人有任何链接或例子吗?我被难住了:) 谢谢您可以将响应保存到html文件中,然后用命令启动浏览器以打开该文件 class Program { static int Main() { WebRequest wr = HttpWebRequest.Cre

我是c#新手,正在制作一个简单的代理来编辑某些标题

我使用HttpLisenter获取请求,然后使用HttpWebRequest和Response进行编辑、发送和接收

现在,我需要一些帮助来将编辑后的响应发送回web浏览器。有人有任何链接或例子吗?我被难住了:)


谢谢

您可以将响应保存到html文件中,然后用命令启动浏览器以打开该文件

class Program
{
    static int Main() {
        WebRequest wr = HttpWebRequest.Create("http://google.com/");
        HttpWebResponse wresp = (HttpWebResponse)wr.GetResponse();

        string outFile = @"c:\tmp\google.html";

        using (StreamReader sr = new StreamReader(wresp.GetResponseStream()))
        {
             using(StreamWriter sw = new StreamWriter(outFile, false)) {
                  sw.Write(sr.ReadToEnd());
             }
        }

        BrowseFile(outFile);

        return 0;
   }

   static void BrowseFile(string filePath)
   {
       ProcessStartInfo startInfo = new ProcessStartInfo();
       startInfo.FileName = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
       startInfo.Arguments = filePath;
       Process.Start(startInfo);
   }
}

看看现有代理服务器的源代码

  • (c#)
  • (c#)

    • 以下是一个简短的示例:

      public void ProcessRequest(HttpContext context)
      {
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create([NEW_URL]);
          request.Timeout = 1000 * 60 * 60;
      
          request.Method = context.Request.HttpMethod;
      
          if (request.Method.ToUpper() == "POST")
          {
              Stream sourceInputStream = context.Request.InputStream;
              Stream targetOutputStream = request.GetRequestStream();
              sourceInputStream.CopyTo(targetOutputStream);                
          }
      
          HttpWebResponse response = (HttpWebResponse)request.GetResponse();
          context.Response.ContentType = request.ContentType;
      
          using (response)
          {
              Stream targetInputStream = response.GetResponseStream();
              Stream sourceOutputStream = context.Response.OutputStream;
              targetInputStream.CopyTo(sourceOutputStream);                
          }
      }
      
      这假设定义了以下扩展方法(我认为使用它可以使示例更具可读性):


      谢谢你的回复。。。这是一个好主意,但并不能真正满足我的要求。如果我不能使用HttpWebResponse,是否有其他功能将数据发送到web浏览器?谢谢这是一个我无法提供代码的难题。您必须接受来自网络上web浏览器的传入http请求,代表浏览器发出http请求,并将响应返回到原始通道。是的,是将响应返回到web浏览器导致了问题。我是否可以使用套接字将web浏览器连接到我的应用程序,然后使用HttpWebRequest/Response从发送的数据中获取数据,然后以这种方式将其发送回去?如果有的话,有人可以发布链接或示例吗??感谢againOk,我已经成功地用HttpWebResponse将响应HTML发送回浏览器,但是图像会显示吗?浏览器如何读取图像?浏览器需要什么格式才能显示图像(二进制,base64),还是我做错了?非常感谢
      public static void CopyTo(this Stream input, Stream output)
      {
          using (input)
          using (output)
          {
              byte[] buffer = new byte[1024];
              for (int amountRead = input.Read(buffer, 0, buffer.Length); amountRead > 0; amountRead = input.Read(buffer, 0, buffer.Length))
              {
                  output.Write(buffer, 0, amountRead);
              }                
          }
      }