javascript - Cannot read InputStream from HttpListenerRequest -
i've been creating download manager receive file name , download link chrome extension.
in case decided use javascript background page send xmlhttprequest loopback , create simple server receive message background page this.
background.js
function showmessagebox(info, tab) { var link = decodeuricomponent(info.linkurl); var index = link.search(/[^/\\\?]+\.\w{3,4}(?=([\?&].*$|$))/); var filename = link.substring(index); alert("will download " + link + " soon\n file name : " + filename); sendmessage(filename,link); } function sendmessage(filename, link) { var xhr = new xmlhttprequest(); xhr.open("post","http://localhost:6230", false); xhr.setrequestheader("content-type", "application/json; charset=utf-8"); var jsonstring = json.stringify({ filename: filename, downloadlink: link }); xhr.send(jsonstring); }
and part of server.
private void startreciever() { while (true) { var fileinfo = getfileinfo(); execute.onuithread(() => windowmanager.showdialog( new newdownloadviewmodel(windowmanager, eventaggregator, fileinfo, engine))); } } private fileinfo getfileinfo() { using (var listener = new httplistener()) { listener.prefixes.add("http://localhost:6230/"); listener.start(); var requestcontext = listener.getcontext(); /*var streamreader = new streamreader(requestcontext.request.inputstream, requestcontext.request.contentencoding); string jsonstring = streamreader.readtoend();*/ var stream = requestcontext.request.inputstream; byte[] buffer = new byte[10240]; var readbyte = stream.read(buffer, 0, 102400); string ss = encoding.utf8.getstring(buffer, 0, readbyte); // deserialize string object return new fileinfo(); } }
i wonder why stream read return 0 when message sent.
finally, found forgot!
according same-origin policy, can't send http request server isn't same origin.
must use cross-origin xmlhttprequest communicate server isn't same origin.
to need request cross-origin permissions add hostname permissions section.
in case this.
"permissions": [ "contextmenus", "http://localhost/", "nativemessaging" ]
now, can receive string , deserialize it.
Comments
Post a Comment