如何在 C# 中使用 restful web 服务发送数据

How to sent data with restfull web service in C#

我正在开发 Windows Phone 8.1 应用程序。 我是 C# 和 WP 的新手。我为 sql 服务器连接使用了 restfull 网络服务,但我无法将数据发送到服务器。我收到一条错误消息 "Bad Request".

这是我的登录页面代码bihend

KullaniciManager km = new KullaniciManager();
            km.Login();
            HttpClient httpClient = new System.Net.Http.HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:3577/KullaniciService.svc/Login");
            HttpResponseMessage response = await httpClient.SendAsync(request);

            MessageDialog msgbox = new MessageDialog("Serverdan gelecek hata mesajı");
            await msgbox.ShowAsync();

我的BLL代码在这里。

 public LoginResponse KullaniciKontrolEt(string kulAdi, string sifre)
    {
        LoginResponse response = null;
        using (NeydiolilacEntities noi = new NeydiolilacEntities())
        {
            object data = noi.ta_Kullanici.Where(x => x.Kul_Ad == kulAdi && x.Kul_Sifre == sifre && x.Kul_Statu == true).SingleOrDefault();

            response = new LoginResponse()
            {
                Data = data
            };

            return response;
        }

感谢您的帮助:)

*

Hi Asim,
This will help you I hope

注:Win8.1的代码

public async Task<string> GeneralRequestHandler(string RequestUrl, object ReqObj)
        {
            try
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(ReqObj);
                HttpContent content = new StringContent(json);
                Windows.Web.Http.IHttpContent c = new Windows.Web.Http.HttpStringContent(json);
                c.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/json");
                Windows.Web.Http.Filters.HttpBaseProtocolFilter aHBPF = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();

                aHBPF.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.Untrusted);
                aHBPF.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.InvalidName);
                string responseText;
                using (var handler = new Windows.Web.Http.HttpClient(aHBPF))
                {
                    Windows.Web.Http.HttpResponseMessage r = await handler.PostAsync(new Uri(RequestUrl), c);
                    responseText = await r.Content.ReadAsStringAsync();
                }
            }
            catch (HttpRequestException ex)
            {

            }

            return responseText;
        }

*