Web services 会话启用和无cookieless与身份验证有什么关系?

Web services 会话启用和无cookieless与身份验证有什么关系?,web-services,Web Services,好吧,我们在这件事上纠缠了2天,现在还不知道 我们有一个.asmx web服务(配置为cookieless=“true”): 我们尝试从两个方面调用它:c#表单应用程序,如下所示: private void button1_Click(object sender, EventArgs e) { string url = "http://192.168.5.223:8989/MyAPI.asmx/SimplestWebService";

好吧,我们在这件事上纠缠了2天,现在还不知道

我们有一个.asmx web服务(配置为cookieless=“true”):

我们尝试从两个方面调用它:c#表单应用程序,如下所示:

 private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://192.168.5.223:8989/MyAPI.asmx/SimplestWebService";
            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json; charset=utf-8";
            req.UseDefaultCredentials = true;
            req.PreAuthenticate = false;
            req.ContentLength = 0;
            var result = new StringBuilder();
            using (HttpWebResponse res = (HttpWebResponse) req.GetResponse())
            {
                StreamReader sr = new StreamReader(res.GetResponseStream());
                result.Append(sr.ReadToEnd());
            }
            label1.Text = result.ToString();
        }

 function simplestMethod() {
var callParams = "{}" $.ajax({ type: "POST", url: "http://192.168.5.223:8989/MyAPI.asmx/SimplestWebService", data: callParams, contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { alert(data.d); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Error Occured!" + " | " + XMLHttpRequest.responseText + " | " + textStatus + " | " + errorThrown); } }); }

私有无效按钮1\u单击(对象发送者,事件参数e)
{
字符串url=“”;
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(url);
请求方法=“POST”;
req.ContentType=“application/json;charset=utf-8”;
req.UseDefaultCredentials=true;
req.PreAuthenticate=false;
req.ContentLength=0;
var result=新的StringBuilder();
使用(HttpWebResponse res=(HttpWebResponse)req.GetResponse())
{
StreamReader sr=新的StreamReader(res.GetResponseStream());
result.Append(sr.ReadToEnd());
}
label1.Text=result.ToString();
}

还有Jquery,就像这样:

 private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://192.168.5.223:8989/MyAPI.asmx/SimplestWebService";
            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json; charset=utf-8";
            req.UseDefaultCredentials = true;
            req.PreAuthenticate = false;
            req.ContentLength = 0;
            var result = new StringBuilder();
            using (HttpWebResponse res = (HttpWebResponse) req.GetResponse())
            {
                StreamReader sr = new StreamReader(res.GetResponseStream());
                result.Append(sr.ReadToEnd());
            }
            label1.Text = result.ToString();
        }

 function simplestMethod() {
var callParams = "{}" $.ajax({ type: "POST", url: "http://192.168.5.223:8989/MyAPI.asmx/SimplestWebService", data: callParams, contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { alert(data.d); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Error Occured!" + " | " + XMLHttpRequest.responseText + " | " + textStatus + " | " + errorThrown); } }); }

函数simplestMethod(){
var callParams=“{}” $.ajax({ 类型:“POST”, url:“”, 数据:callParams, contentType:“应用程序/json;字符集=utf-8”, 数据类型:“json”, 成功:功能(数据){ 警报(数据d); }, 错误:函数(XMLHttpRequest、textStatus、errorshown){ 警报(“发生错误!”+“|”+XMLHttpRequest.responseText+“|”+ text状态+“|”+错误抛出); } }); }

表单应用程序产生以下错误:
远程服务器返回错误:(401)未经授权。
但是当删除contentType行时-返回有效的输出-但是使用XML(而不是json..,这是显而易见的)

Jquery(与web服务在同一台机器上)给出:

当我们在非cookieless模式或Sessionabled=false的情况下使用它们时,两者都能完美地工作。但这不是我们需要的。
我还应该提到,我们使用的是IIS 6.0和.NET 4.0。 最后一件事——如果我们直接转到web服务链接(使用浏览器),它会工作,但会返回XML(而不是json)

任何关于如何使用cookieless=trueEnableSession=true进行此操作的建议都将非常感谢


谢谢。

在以下位置找到一些代码后,我成功地实现了这一点:

更具体地说,下面的调用对我有效,因为我无法让我在页面中看到的第一个示例对我有效。它使用URL中的SessionId正确生成服务的URL,以便服务器可以正确查找您的会话

Sys.Net.webservicecoproxy.invoke('WebService.asmx','HelloWorld',false,{},SucceededCallback,null,“用户上下文”,1000000)

有关此电话的信息,请访问:

真的很遗憾,在MSDN网站上,我能找到的任何地方都没有涉及到这个场景…上面的页面是我能找到的唯一一个,有一个工作代码示例

丰富的