Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从WinForms应用程序将JSON发布到mvc3控制器_C#_Asp.net Mvc_Winforms - Fatal编程技术网

C# 从WinForms应用程序将JSON发布到mvc3控制器

C# 从WinForms应用程序将JSON发布到mvc3控制器,c#,asp.net-mvc,winforms,C#,Asp.net Mvc,Winforms,我在ASP.NET MVC3应用程序中有一个控制器/操作。它处理用户登录的post请求。我试图通过桌面winForms应用程序模拟它 我的控制器如下所示: [HttpPost] [AllowAnonymous] public virtual ActionResult LogOn(LogOnModel model, string returnUrl) { //authenticate user logic } 我通过以下方式生成HTTP请求: 公共静态bool Aut

我在ASP.NET MVC3应用程序中有一个控制器/操作。它处理用户登录的post请求。我试图通过桌面winForms应用程序模拟它

我的控制器如下所示:

  [HttpPost]
  [AllowAnonymous]
  public virtual ActionResult LogOn(LogOnModel model, string returnUrl)
  {
    //authenticate user logic
  }
我通过以下方式生成HTTP请求:

公共静态bool AuthenticateClient(客户端、字符串用户名、字符串密码) { //将这些字段名更改为登录页面所需的任何元素 字符串usernameField=用户名; 字符串密码字段=密码

        string loginUrl = "http://localhost/Account/LogOn/";

        // format login request
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(loginUrl);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.AllowAutoRedirect = false;
        request.CookieContainer = new CookieContainer();

        // format and send login data
        string requestData = JSON.Serialize(new LogOnModel(usernameField, passwordField, false));

        StreamWriter sw = new StreamWriter(request.GetRequestStream());
        sw.Write(requestData);
        sw.Close();

        // get the login response
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream());
        string responseData = sr.ReadToEnd();
        sr.Close();

        // retrieve the session ID
        string sessionId = null;
        foreach (Cookie cookie in response.Cookies)
        {
            if (cookie.Name == AuthCookieName)
            {
                sessionId = cookie.Value;
            }
        }

        // could not authenticate
        if (sessionId == null)
        {
            return false;
        }

        // send the session ID with every request
        client.OnRequestCreated = (args) =>
        {
            args.Request.Headers["Cookie"] = string.Format("{0}={1}", AuthCookieName, sessionId);
        };
        return true;
    }
通常,我可以从jQuery发出Ajax请求,并向其传递控制器操作所期望的相同对象参数(本例中为LogonModel),但是,当我从桌面应用程序执行此操作时,每次在操作中它都显示为null

因此,我的问题是,如何从桌面winforms应用程序发出请求,并在控制器中填充对象(LogonModel),以便进行身份验证?

(为了完整性…)


你永远也不可能太确定你是否正确地构建了请求框架。就我个人而言,我发现使用然后根据需要优化和模拟来监视和比较调用更容易…

代码解决方案:string requestData=string.Format(“{0}={1}&{2}={3}&RememberMe=false&Login=Login”,“UserName”,Uri.EscapeDataString(UserName),“Password”,Uri.EscapeDataString(Password));