[英]Is is possible to send information and receive a response through wcf/webservice to a website from a wpf app
我想從我的wpf應用程序向我的mvc4網站發送一些數據,並從網站接收回我的wpf應用程序的數據。 在來到這里之前,我曾試圖找到一種解決方案,而有人說關於Web服務的功能應該可以做到這一點,但是我不知道我是否需要在mvc4網站中創建Web服務,或者是否需要創建wcf項目。在我的WPF應用中。
任何有關此的指南或教程將不勝感激。
編輯:這是我看到第一個解決方案目前無法解決之后的結果,我不確定為什么即時通訊沒有收到適當的回復。
public bool GetData(LoginObj loginObj)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:12611/");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("Home/VerifyLogin",loginObj).Result;
if (response.IsSuccessStatusCode)
{
var users = response.Content;
if (users.ToString() == "true")
return true;
else
return false;
}
return false;
}
這在下面的家庭控制器中
public class LoginObj
{
public string username { get; set; }
public string password { get; set; }
public LoginObj(string username, string password)
{
this.username = username;
this.password = password;
}
}
public JsonResult VerifyLogin(LoginObj loginObj)
{
bool isValid = true;
isValid = System.Web.Security.Membership.ValidateUser(loginObj.username, loginObj.password);
return Json(isValid,JsonRequestBehavior.AllowGet);
}
我不確定您要做什么,但是通常Web應用程序(mvc4)會調用該服務。 無論發送,接收或兩者兼而有之。
在那里,您的Web應用程序調用了wcf請求數據,如果在其他地方進行了更改,則無論如何它都會從wcf返回。 當您需要保存數據時,只需將其發送到wcf。
如果您需要一些類似於通知/聊天的應用程序,以便WCF可以將數據推送到您的Web應用程序,似乎您正在尋找SignalR。
如果可以,請擴展或更好地解釋您的問題/需求。
更新:
您可以創建一個“ Web服務”(WCF或WebAPI),也可以使用已經存在的MVC應用程序,並返回一個json響應以充當Web服務。
您可以獲得的簡單內容如下:
您的MVC應用中的新操作
public JsonResult CanLogin(string username, string password){
bool isValid = true; // check if username & password are ok and return True or False
return Json(isValid);
}
然后在您的wpf上創建一個httpclient並使用用戶名和密碼調用/ application / CanLogin
您可以在瀏覽器的json響應上允許GetGet進行測試。
return Json(isValid, JsonRequestBehavior.AllowGet);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.