如何使用C#中的restful Web服务发送数据

时间:2015-04-17 07:16:38

标签: c# sql-server web-services rest windows-phone-8

我正在开发Windows Phone 8.1应用程序。 我是C#和WP的新手。我使用restfull Web服务进行sql server连接,但我无法将数据发送到服务器。我收到了错误消息" 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;
        }

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

*

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;
        }

*