简体   繁体   中英

Calling a web method from Windows Phone 8

I have an ASP.NET Web Form application.

I also have a HTML5 Web App to call the web methods on my code behind page.

A typical method defined in my asp code behind page is:

//in code behind login.aspx page

[WebMethod]
public static string Login(string username, string password)
{
   //do something
}

In my HTML5 Web App I would call it like:

        jQuery.support.cors = true;
        $.ajax({
            crossDomain: true,
            type: "POST",
            url: "Login.aspx/Login",
            data: JSON.stringify({
                username: usernameentered,
                password: passwordentered
            }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                $(msg.d).each(function () {
                    if (this['Success'] == 1) {
                       //process login 
                    }
                    else {
                       //process server error
                    }
                });
            },
            error: function (msg) {
              //handle error
        });
    });

Now bearing in mind I do not want to move to web API and MVC at this time (will do eventually) and also I would prefer not to call a web service (asmx) or WCF service how can I still call this web method from that page in the way that i do through JavaScript but via C# Windows Phone 8?

You can use HttpClient

using (var client = new HttpClient())
{
    var resp = await client.PostAsJsonAsync("http://your host/Login.aspx/Login", 
                                             new { username = "", password = "" });

    var str = await resp.Content.ReadAsStringAsync();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM