从JavaScript使用asp.net和C#以外的服务器上调用Web服务 [英] Call webservice on outside server from javascript using asp.net and C#

查看:164
本文介绍了从JavaScript使用asp.net和C#以外的服务器上调用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试使用创建用户名和密码字段和提交按钮形式的ASP.NET页面的Web服务调用。 (jQuery的都和js文件我使用都包含在head元素脚本标记。)

I'm trying to test web service calls using an ASP.NET page that creates a form with username and password fields and a "Submit" button. (Both jQuery and the .js file I'm using are included in script tags in the head element.)

提交按钮调用在C#创建的函数后面的文件,使一个单独的JavaScript文件调用的代码。

The "Submit" button calls a function created in the C# code behind file that makes a call to a separate JavaScript file.

protected void mSubmit_Click(object sender, EventArgs eventArgs)
{
    String authenticate = String.Format("Authentication(\"{0}\",\"{1}\");", this.mUsername.Text,this.mPassword.Text);
    Page.ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", authenticate, true);
}



JavaScript函数,身份验证,使得Web服务调用,使用jQuery和Ajax,到不同的服务器,发送JSON参数和响应期待回JSON。

The JavaScript function, Authenticate, makes web service call, using jQuery and Ajax, to a different server, sending JSON parameters and expecting back JSON in response.

function Authentication(uname, pwd) {

    //gets search parameters and puts them in json format
    var params = '{"Header":{"AuthToken":null,"ProductID":"NOR","SessToken":null,"Version":1},"ReturnAuthentication":true,"Password":"' + pwd + '","Username":"' + uname + '","ReturnCredentials":false }';

    var xmlhttp = $.ajax({
        async: false,
        type: "POST",
        url: 'https://myHost.com/V1/Identity/Authenticate',
        data: params,
        contentType: 'application/json'
    });

    alert(xmlhttp.statusText);
    alert(xmlhttp.responseText);

    return;
}



不过,因为我调用Web服务是在不同的服务器上比ASP.NET的,C#和JavaScript文件,我没有得到一个状态文本的responseText 警报。

不知何故,没有被发送到Web服务,我没有得到任何东西,甚至不是一个错误。我试图把在 beforeSend 属性的功能,但没有开枪。有一种特殊的方式,我需要处理调用一个关闭服务器的Web服务?

Somehow, nothing is being sent to the web service and I'm not getting anything back, not even an error. I tried putting a function in the beforeSend attribute, but that didn't fire. Is there a special way I need to handle calling an off-server web service?

更新!

目前jjnguy,珍妮和内森的建议,我现在尝试使用HttpWebRequest的服务器端调用Web服务。使用某些jjnguy的代码,以及从这个问题,我想出了这一点。

At the advice of jjnguy, Janie and Nathan, I'm now trying a server side call to the web service using HttpWebRequest. Using some of jjnguy's code as well as code from this question, I've come up with this.

public static void Authenticate(string pwd, string uname)
{
    string ret = null;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://myhost.com/V1/Identity/Authenticate");
    request.ContentType = "application/json";
    request.Method = "POST";

    string data = "{\"Header\":{\"AuthToken\":null,\"ProductID\":\"NOR\",\"SessToken\":null,\"Version\":1},\"ReturnAuthentication\":true,\"Password\":\"" + pwd + "\",\"Username\":\"" + uname + "\",\"ReturnCredentials\":false }'";

    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
    request.ContentLength = byteData.Length;

    using (Stream postStream = request.GetRequestStream()) 
    {
        postStream.Write(byteData, 0, byteData.Length);
    }

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    using (response)
    {
        // Get the response stream  
        StreamReader reader = new StreamReader(response.GetResponseStream());

        // Console application output  
        ret = reader.ReadToEnd();
    }

    Console.WriteLine(ret);
}



不过,我得到一个(400)从当我尝试从我的HttpWebRequest响应远程服务器错误请求错误。异常的响应属性的值表示 {System.Net.HttpWebResponse} 和Status属性的值为 ProtocolError 。我敢肯定,这是因为URL是使用HTTP SSL协议。我能做些什么来解决这个问题,除了具有ASP.NET页面的网址开头是https(而不是一个选项)?

However, I'm getting a (400) Bad Request error from the remote server when I try to get the response from my HttpWebRequest. The value of the Response property of the exception says {System.Net.HttpWebResponse} and the value of the Status property is ProtocolError. I'm pretty sure this is because the URL is using HTTP SSL protocol. What can I do to get around that, other than having the ASP.NET page URL start with HTTPS (not an option)?

推荐答案

原来,我贴在我的更新代码是正确的,我只是有一个错字,一个在数据串设置不正确。

Turns out the code that I posted in my update was correct, I just had a typo and one setting incorrect in the data string.

    string data = "{\"Header\":{\"AuthToken\":null,\"ProductID\":\"NOR\",\"SessToken\":null,\"Version\":1},\"ReturnAuthentication\":true,\"Password\":\"" + pwd + "\",\"Username\":\"" + uname + "\",\"ReturnCredentials\":true}";

这篇关于从JavaScript使用asp.net和C#以外的服务器上调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆