The Web page communicates with the local computer

Source: Internet
Author: User

It was a long, long time ago. The Web page communicates with the customer's local computer localhost in two ways:

1. Flash 2. ActiveX controls

Because flash I do not know very well, also can not give any sample code,

For ActiveX controls, you can search for "ActiveX controls" directly on the web, and there are a number of related answers

But:

  For modern browsers, both of these methods are not supported.

For Flash, although the browser is still supported, but it is not the default loading flash plugin

To be loaded by the user by hand with a point of consent.

For ActiveX controls, only in IE browser is supported, but also user consent, not to mention the modern browser

So. For a modern browser. To communicate with the local computer. The above two ways are no longer feasible.

Is it possible to communicate with the local computer directly through a Web page without the user's consent?

The answer is of course there is. Otherwise, there will be no such article.

For web-to-server communication, we are going to the HTTP protocol. This is also supported by all browsers
It's not even a browser or an ancient browser supports the HTTP protocol.

So

If the web is to communicate with the local computer, as long as the web and the local computer Go HTTP protocol communication. Does it solve the problem?

How does that make it?

Since we are going to communicate with the local computer on the HTTP protocol, there must be a channel that supports the HTTP protocol
Usually we communicate with the remote server How to access, for example, Baidu Bar, we input http://www.baidu.com will be able to open the normal
What do we do in the local, when we do the development test, is not input: http://localhost or http://127.0.0.1 is not access to our local computer
So, we send a request locally to the page. What's the result?

Let's experiment:

First, since you want to communicate with the local server. So we're going to listen to local HTTP protocol traffic locally, the code is as follows

namespacelocalapp{classProgram { Public StaticHttpListener listener =NewHttpListener (); Static voidMain (string[] args) {Listener.            Start (); Listener. Prefixes.add ("http://127.0.0.1:8976/");//We listen to the HTTP protocol on the local computer 8976 PortThread T =NewThread (NewThreadStart (Clientlistener));            T.start ();  while(true)            {                strings =Console.ReadLine (); Listener.                Stop (); Listener.            Close (); }        }         Public Static voidClientlistener () { while(true)            {                Try                {                    //If we get the emphasis, we open up a thread to process the request.HttpListenerContext request =Listener.                    GetContext ();                ThreadPool.QueueUserWorkItem (ProcessRequest, request); }                Catch(Exception e) {Console.WriteLine (e.message);} }        }         Public Static voidProcessRequest (ObjectListenercontext) {            Try            {                varContext =(HttpListenerContext) Listenercontext; varDicpar =Newdictionary<string,string>(); varListpar =Newlist<string>(); //GET Request Parameters                foreach(varIteminchcontext. Request.QueryString.AllKeys) {Listpar.add (String.Format ("{0}={1}", item, context.                    Request.querystring[item])); Dicpar.add (item, context.                Request.querystring[item]); }                //set the return value                varResultjson =Jsonconvert.serializeobject (Dicpar);                Console.WriteLine (String.Join (Environment.NewLine, Listpar)); Context. Response.statuscode= (int) Httpstatuscode.ok; Context. Response.contentlength64=System.Text.Encoding.UTF8.GetByteCount (Resultjson); Context. Response.ContentType="Application/json"; Context. Response.ContentEncoding=Encoding.UTF8; Context. RESPONSE.HEADERS.ADD ("Access-control-allow-origin","*"); //return ResultsSystem.IO.Stream output =context.                Response.outputstream; using(StreamWriter writer =NewStreamWriter (output)) {writer.                    Write (Resultjson); Writer.                Close (); }            }            Catch            {            }        }    }}

The code above is to listen to the local computer 8976 port HTTP protocol,
If a request is received. Extracts the parameters and returns them as JSON.

So, what happens when we write an HTML page that sends data to a local 8976 port
The code is as follows:

<!DOCTYPE HTML><HTML><Head>    <MetaCharSet= "Utf-8" />    <title></title>    <Scriptsrc= "Scripts/jquery-3.3.1.js"></Script></Head><Body>    <formID= "Form1"enctype= "application/x-www-form-urlencoded">        <Div><label>Parameter 1:<inputtype= "text"name= "Par1" /></label></Div>        <Div><label>Parameter 2:<inputtype= "text"name= "Par2" /></label></Div>        <Div><Buttontype= "button"onclick= "btnsubmit ()">Submit</Button></Div>    </form>    <Script>        functionbtnsubmit () {$.get ("http://127.0.0.1:8976/", $("#form1"). Serialize (),function(Result) {Console.log (result); }, "JSON"); }    </Script></Body></HTML>

Open a Chrome browser to do the test. The results are as follows:


The page successfully initiated the request, the local computer app also heard the web sent a request, parameters are also received.
Is it possible for us to use web-side to invoke the resources of the local computer in this way?

To know the question:

When our site is on HTTPS access, if you use HTTP to request local resources, instead of using HTTPS to request, some browsers will report mixed content error
But in the new version of Google and Firefox has solved this problem, refer to: https://bugzilla.mozilla.org/show_bug.cgi?id=903966


The other one has a secret that the average person I don't tell him
You know when you log in locally QQ, and then open the QQ Web site, why he can automatically know your current login QQ?

Yes, he also used this program with QQ communication, so that you log on which QQ, so that there will be this fast login function

Thinking Extension:
Such a visit can only be a web-initiated request, and the local computer accepts the request. only supports single-direction communication,
So, is there a solution that supports two-way communication? (Web to local, local to web)
If there is one who knows the answer. You can say what you think in this post.

The Web page communicates with the local computer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.