2015-04-17 63 views
0

我正在開發Windows Phone 8.1應用程序。 我是C#和WP中的新手。我使用restfull的Web服務的SQL服務器連接,但我不能發送數據到服務器。我的錯誤消息是「錯誤的請求」。如何在C中使用restfull web服務發送數據#

這是我的登錄頁面代碼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; 
     } 

感謝您的幫助:)

回答

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

*

相關問題