使用 http-proxy 代理 HTTP 请求时遇到的 the requested url is invalid 错误消息
使用如下代码创建 HTTP 代理服务器:
const http = require('http');
const httpProxy = require('http-proxy');
const targetUrl = 'https://www.sap.cn/index.html';
const proxy = httpProxy.createProxyServer({
target: targetUrl,
secure:false
});
http.createServer(function (req, res) {
proxy.web(req, res);
}).listen(8089);
console.log('Proxy listens in 8089');
浏览器输入 http://localhost:8089/,遇到如下错误消息:
The requested URL “http://%5bNo%20Host%5d/index.html/”, is invalid.
在 proxy 服务器构造时,添加一行 changeOrigin:true,
后,错误消失:
这行代码的作用:
changeOrigin: true/false, Default: false - changes the origin of the host header to the target URL
意思是:置为 true,可以将 host header 的 origin 值,更改为目标 URL。
我们可以把 HTTP server 构造时指定的 target 字段,设置到 proxy.web 方法里,仍然工作:
第 15 行 web 方法的第三个参数,接收一个字段为 target
的 JSON 对象,值为期望跳转到的目标 url.
同第一种方法不同,大家注意到,这种方法,我们在地址栏里输入了 localhost:8089, 打开被代理的百度网页后,地址栏里的 localhost:8089 保持不变:
const http = require('http');
const httpProxy = require('http-proxy');
const targetUrl = 'https://www.sap.cn/index.html';
const baidu = 'https://www.baidu.com';
const proxy = httpProxy.createProxyServer({
//target: targetUrl,
changeOrigin:true,
secure:false
});
http.createServer(function (req, res) {
proxy.web(req, res, {
target: baidu
});
}).listen(8089);
console.log('Proxy listens in 8089');
但是对于 sap 官网来说,有一个重定向的行为:
Access to fetch at ‘https://wappass.baidu.com/static/captcha/tuxing.html?&logid=11334581951689513341&ak=c27bbc89afca0463650ac9bde68ebe06&backurl=https%3A%2F%2Fwww.baidu.com%2Fbaidu&signature=266d81f92ed0df604946084fd4d93f4c×tamp=1661502581’ (redirected from ‘http://localhost:8085/baidu’) from origin ‘http://localhost:8085’ has been blocked by CORS policy: The ‘Access-Control-Allow-Origin’ header has a value ‘http://wappass.baidu.com’ that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
正常情况下,使用 fetch 请求绝对路径:
在 Chrome 开发者工具 network 标签页里,没有观察到 OPTIONS 请求:
直接就是 HTTP GET CORS 错误了。当站点 A 尝试从站点 B 获取内容时,站点 B 可以发送一个 Access-Control-Allow-Origin 响应标头,告诉浏览器该页面的内容可以从某些来源访问。 源是一个域(domain),加上一个方案(scheme)和端口号。
相关文章
- IIS部署ASP.NET MVC (4.0)网站出现的错误
- 《Sqlserver》通过端口 8080 连接到主机 localhost 的 TCP/IP 连接失败。错误:“驱动程序收到意外的登录前响应。请验证连接属性,并检查 SQL Server 的实例正在主机上运行,且在此端口接受
- Cannot set a credential for principal 'sa'. (Microsoft SQL Server,错误: 15535)
- Java实现 LeetCode 278 第一个错误的版本
- 错误 内存溢出 vendor/composer/autoload_real.php on line 66
- 【解决】oracle报IMP-00058与IMP-00000错误
- 【解决】oracle使用impdp导入数据库提示ORA-14460错误
- 外部系统调用PO的webservice服务,往SAP系统发送数据,用的XISOAPAdapter的方式,出现错误
- Java错误消息sun.security.validator.ValidatorException应该如何处理
- 如何处理Docker的错误消息request canceled:Docker代理问题
- 完美解决Ubuntu下无法获得锁 / 检测到系统程序错误 / E: Could not get lock /var/lib/apt/lists/lock
- PSPnet:Pyramid Scene Parsing Network——作者认为现有模型由于没有引入足够的上下文信息及不同感受野下的全局信息而存在分割出现错误的情景,于是,提出了使用global-scence-level的信息的pspnet
- 解决“错误D8016“/ZI”和“/Gy-”命令行选项不兼容”问题
- 使用root用户通过SSH登录Linux实例时报“Permission denied, please try again”的错误