通过代理访问远程应用程序网页

时间:2019-05-13 06:29:50

标签: c# asp.net-mvc http-proxy

有人可以指导我在asp.net mvc应用程序中创建代理以访问远程应用程序网页并将其加载到我的mvc应用程序中的iframe中

这就是我尝试创建代理服务器并访问其他应用程序的html页面以编辑样式的方式。

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:4000/sample.html?user=root&lang=en");
                WebProxy myproxy = new WebProxy("http://localhost:9999/sample.html);
                request.Proxy = myproxy;
                request.Method = "GET";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();`
  string contentType = response.ContentType;
            Stream content = response.GetResponseStream();
           var doc = new HtmlDocument();
            doc.Load(content);
            Response.ContentType = contentType;
            doc.Save(Response.OutputStream);
            return Content("");

现在我应该让iframe指向代理服务器

 <iframe id="ifrm" src="http://localhost:9999/sample.html?user=root&lang=en" frameborder="0" width="700" height="700"></iframe>

我也可以通过下面的代码片段通过node.js来做到这一点。但是通过C#iam无法加载iframe

const REMOTE_IP = 'http://localhost:4000';
const express = require('express')
const app = express()
const port = 3000
app.get('/api', (req, res) => res.send('Test data'));
app.use(express.static('public'));
app.listen(port, () => {
  });
const proxy = require('http-proxy-middleware');
const proxyServer = express();
proxyServer.use('/sample.html', proxy({ target: REMOTE_IP, selfHandleResponse: true, onProxyRes: (proxyRes, req, res) => {
    let body = new Buffer('');
           proxyRes.on('data', (data) => body = Buffer.concat([body, data]));
   proxyRes.on('end', function () {
        const htmlString = body.toString();
        const lines = htmlString.split('\n');
          lines[21] = `<link href="http://localhost:3000/mystyle.css" rel="stylesheet">`;      
  res.write(lines.join('\n'));
                  res.end();
    });
} }));
proxyServer.use('/', proxy({ target: REMOTE_IP, changeOrigin: true }));
proxyServer.listen(9999);

0 个答案:

没有答案