【问题标题】:MVC website doesn't receive string from clientMVC 网站未收到来自客户端的字符串
【发布时间】:2017-09-02 16:54:28
【问题描述】:

客户:

WebClient wc = new WebClient();
try
{
    string json = wc.UploadString("http://localhost:50001/Client/Index", "1");
    dynamic receivedData = JsonConvert.DeserializeObject(json);
    Console.WriteLine("Result: {0};",receivedData.data);
}
catch (Exception e)
{
    Console.WriteLine("Oh bother");
    Console.WriteLine();
    Console.WriteLine(e.Message);
}

基本上将“1”发送到Client 控制器中的Index 操作。

这里是控制器:

[HttpPost]
public ActionResult Index(string k)
{
  Debug.WriteLine(String.Format("Result: {0};", k));
  return Json(new { data = k}, JsonRequestBehavior.DenyGet);
}

Client 的结果只是“结果:;”。控制器的调试输出也是“Result: ;”。这意味着数据在客户端和站点之间的某处丢失。但是当我调试时,Visual Studio 说有一个请求。

【问题讨论】:

  • @TKHN 你认为这是因为我使用 UploadString 而不是 UploadData,它应该是一个字节数组吗?
  • @TKHN 我尝试了 byte[] 但 NullException 出现了。

标签: c# asp.net-mvc json.net


【解决方案1】:

通过添加标头并指定参数名称,我已设法使其工作(在您的调用方法中):

 static void Main(string[] args)
        {
            WebClient wc = new WebClient();
            try
            {
                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                string json = wc.UploadString("http://localhost:49847/Home/Index", "k=1");
                dynamic receivedData = JsonConvert.DeserializeObject(json);
                Console.WriteLine("Result: {0};", receivedData.data);
            }
            catch (Exception e)
            {
                Console.WriteLine("Oh bother");
                Console.WriteLine();
                Console.WriteLine(e.Message);
            }
        }

来自MSDN

...将 HTTP Content-Type 标头设置为 application/x-www-form-urlencoded, 通知服务器表单数据已附加到帖子。

我没有运行 fiddler 来检查默认情况下通过什么(如果有的话)发送标头,但我怀疑没有标头这不起作用的原因是接收客户端不知道在哪里可以找到查询字符串参数通过传递。

来自another answer on StackOverflow

当收到一个 POST 请求时,你应该总是期待一个“有效载荷”, 或者,在 HTTP 术语中:消息体。消息体本身是 非常没用,因为没有标准(据我所知。也许 应用程序/八位字节流?)格式。正文格式由 内容类型标头。使用 HTML FORM 元素时 method="POST",通常是 application/x-www-form-urlencoded。

【讨论】:

  • 工作正常。非常感谢!
【解决方案2】:

WebAPI 可能会将您的参数解释为 URI 参数。

试试这个:

[HttpPost]
public ActionResult Index([FromBody] string k)
{
    Debug.WriteLine(String.Format("Result: {0};", k));
    return Json(new { data = k}, JsonRequestBehavior.DenyGet);
}

这告诉 WebAPI 期望从请求正文中提取此参数(例如 JSON 后负载)

【讨论】:

  • 不起作用。然而,这是 [System.Web.Mvc.HttpPost]
【解决方案3】:

试试

            string parameters = string.Concat("k=","1");

            string url = "http://localhost:50001/Client/Index";
            using (Var wc = new WebClient()){

            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded;charset=UTF-8";

            string result = wc.UploadString(url, parameters);

            JObject obj = JObject.Parse(result);

            Console.WriteLine("Result: {0};", obj.data);               

            }`

【讨论】:

  • ` 字符串参数 = string.Concat("k=","1");字符串 url = "localhost:50001/Client/Index";使用 (Var wc = new WebClient()){ wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded;charset=UTF-8";字符串结果 = wc.UploadString(url, 参数); JObject obj = JObject.Parse(result); Console.WriteLine("结果:{0};", obj.data); } `
【解决方案4】:

您需要稍微更改您的操作以从请求流中获取发布的值:

[HttpPost]
public ActionResult Index(string k)
{      
    var stream = Request.InputStream;
    string value = string.Empty;

    using (var reader = new StreamReader(stream))
    {
        value = reader.ReadToEnd();
    }

    Debug.WriteLine(String.Format("Result: {0};", value));
    return Json(new { data =  value}, JsonRequestBehavior.DenyGet);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2012-01-31
    • 2018-10-03
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2019-09-05
    相关资源
    最近更新 更多