简体   繁体   English

如何从 HTTPS 网站向 LocalNetwork 发出 HTTP 请求?

[英]How to make a HTTP request to a LocalNetwork from a HTTPS WebSite?

let's assume that there is a website under HTTPS, in that WebSite the user can set the IP of a local machine (thermal printer in this case).假设在 HTTPS 下有一个网站,用户可以在该网站中设置本地机器(在本例中为热敏打印机)的 IP。 How can the user print something on that printer by using the website from his device?用户如何通过他的设备使用网站在该打印机上打印一些东西?

The user have to use the WebSite only under the local.network where the thermal printer is to be able to print.用户必须仅在热敏打印机能够打印的 local.network 下使用网站。

The printer has a http server in it which it uses for the communication.打印机有一个 http 服务器用于通信。

At this point how can a HTTPS website send a HTTP request to that local thermal printer?此时,HTTPS 网站如何向该本地热敏打印机发送 HTTP 请求?

Assuming you want this to be a variable that any user inputs,and that any user on any.network has the ability to access their own printer, you would need it to be executed on the frontend.假设您希望这是任何用户输入的变量,并且 any.network 上的任何用户都能够访问他们自己的打印机,那么您需要在前端执行它。 This way each user will be able to access the printer on their own.network.这样每个用户都可以在自己的网络上访问打印机。

That gives you basically one option.这基本上给了你一个选择。 Javascript. So if you wanted a stored request with some variables, you could have the javascript store variable from the DOM and then post a fetch request https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch Here is an example function Javascript。所以如果你想要一个带有一些变量的存储请求,你可以从 DOM 中获取 javascript 存储变量,然后发布一个获取请求https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API /Using_Fetch下面是一个例子 function

// Example POST method implementation:
async function postData(url = '', data = {}) {
  // Default options are marked with *
  const response = await fetch(url, {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *cors, same-origin
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *same-origin, omit
    headers: {
      'Content-Type': 'application/json'
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    redirect: 'follow', // manual, *follow, error
    referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
    body: JSON.stringify(data) // body data type must match "Content-Type" header
  });
  return response.json(); // parses JSON response into native JavaScript objects
}

postData('https://example.com/answer', { answer: 42 })
  .then(data => {
    console.log(data); // JSON data parsed by `data.json()` call
  });

You are likely struggling because of CORS. You can find in depth discussion here https://javascript.info/fetch-crossorigin你可能因为 CORS 而苦苦挣扎。你可以在这里找到深入的讨论https://javascript.info/fetch-crossorigin

In short, you will need to modify the fetch headers to have the correct credentials, mode, etc. as seen in the comments in the above snippet.简而言之,您需要修改获取标头以具有正确的凭据、模式等,如上述代码段的注释中所示。

let's assume that there is a website under HTTPS, in that WebSite the user can set the IP of a local machine (thermal printer in this case).假设在 HTTPS 下有一个网站,在该网站中用户可以设置本地机器(本例中为热敏打印机)的 IP。 How can the user print something on that printer by using the website from his device?用户如何通过他的设备使用网站在该打印机上打印一些东西?

The user have to use the WebSite only under the local network where the thermal printer is to be able to print.用户只能在热敏打印机能够打印的本地网络下使用网站。

The printer has a http server in it which it uses for the communication.打印机中有一个 http 服务器,用于通信。

At this point how can a HTTPS website send a HTTP request to that local thermal printer?此时,HTTPS 网站如何向本地热敏打印机发送 HTTP 请求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM