首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpListener问题

HttpListener问题
EN

Stack Overflow用户
提问于 2011-12-14 21:35:34
回答 1查看 2.8K关注 0票数 2

我正在编写一个控制台应用程序作为httplistener,并通过jquery使用它,它是完全正确的,但我想将它转换为https,但我不知道步骤

这是我的代码,我希望任何人都能很快解决它

这是用JScript.js文件编写的jquery代码

代码语言:javascript
复制
 $(function () {
    //this code is executed when the page's onload event fires
    $("#runSample1").click(function () {
        var userNameJS = $("#uName").val();
        var passwordJS = $("#passw").val();
        $.post("http://localhost:80/", { userName: userNameJS, password: passwordJS }, function (data) {
            alert(data);
        });

    });
});

这是html页面

代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.4.1.js"> </script>

    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"> </script>
    <script type="text/javascript" src="Scripts/JScript.js"></script>
    <script type="text/javascript" src="Scripts/jquery-1.4.1-vsdoc.js"></script>
    <style type="text/css">
        .style1 {
            width: 109px;
        }
    </style>
</head>
<body>
<table>

    <tr>
        <td class="style1"><label >User Name</label></td>
        <td><input id="uName"  type="text" /></td>
    </tr>
    <tr>
        <td class="style1"><label >Password</label></td>
        <td><input id="passw"  type="password" /></td>
    </tr>
    <tr>
        <td class="style1"><input id="runSample1"  type="button" value="Send" style="width: 62px"/>  </td>
    </tr>
    </table>
</body>
</html>

并且httplistener代码是

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading;
using System.Collections.Specialized;
using System.Collections;
using System.Security.Cryptography.X509Certificates;
namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            if (!HttpListener.IsSupported)
            {
                Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
                return;
            }
            // Create a listener.
            HttpListener listener = new HttpListener();
            //listener.AuthenticationSchemes = AuthenticationSchemes.Negotiate;
            listener.Prefixes.Add("http://*:8080/");
            //listener.AuthenticationSchemes
            listener.Start();
            Console.WriteLine("Listening...");
            for (; ; )
            {
                HttpListenerContext ctx = listener.GetContext();
                new Thread(new Worker(ctx).ProcessRequest).Start();
            }
        }
    }
    [DataContract]
    internal class Person
    {
        [DataMember]
        internal string name;

        [DataMember]
        internal int age;
    }
    class Worker
    {
        private HttpListenerContext context;
        public Worker(HttpListenerContext context)
        {
            this.context = context;
        }

        public void ProcessRequest()
        {

            HttpListenerRequest request = context.Request;
            List<Person> eList = new List<Person>();
            Hashtable formVars = new Hashtable(); 
            Person person;
            for (int i = 0; i <= 10;i++ )
            {
                person = new Person();
                person.name = "Pesron " + i;
                person.age = i;
                eList.Add(person);
            }
            HttpListenerResponse response = context.Response;
            System.IO.StreamReader reader = new System.IO.StreamReader(request.InputStream, request.ContentEncoding);
            // S contain parameters and values
            string s = reader.ReadToEnd();
            string[] pairs = s.Split('&');
            for (int x = 0; x < pairs.Length; x++)
            {
                string[] item = pairs[x].Split('=');
                formVars.Add(item[0],item[1]);
            }
            String userName = formVars["userName"].ToString();
            String password = formVars["password"].ToString();
            //To send any object as json to client
            DataContractJsonSerializer jsonObject = new DataContractJsonSerializer(typeof(List<Person>));
            System.IO.Stream output = response.OutputStream;
            MemoryStream ms = new MemoryStream();
            jsonObject.WriteObject(ms, eList);
            byte[] buffer = ms.ToArray();
            response.ContentType = "text/plain";
            response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
            response.ContentLength64 = buffer.Length;
            //These headers to allow all browsers to get the response
            response.Headers.Add("Access-Control-Allow-Credentials", "true");
            response.Headers.Add("Access-Control-Allow-Origin", "*");
            response.Headers.Add("Access-Control-Origin", "*");
            //This line to write to the resonse
            output.Write(buffer, 0, buffer.Length);
            output.Close();
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-14 21:56:28

IE不是沙箱本地主机。FF和其他浏览器都这样做。他们认为对localhost的调用是对web的调用,这是不允许的。

您可以尝试添加

代码语言:javascript
复制
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Origin: *

添加到HttpListener的标头。关于为什么需要这样做的一篇好论文可以在here上找到。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8505381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档