如何使用httpclient接收数据,我可以发送数据但不怎么收到它? [英] How to use httpclient to receieve data,I can send data but do not how to recieve it?

查看:90
本文介绍了如何使用httpclient接收数据,我可以发送数据但不怎么收到它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在客户端我编写如下代码:

  static   void  Main( string  [] args)
{
var url = http://www.cnblogs.com/api/say;

var postData = new 字典< string,string>
{
{ 用户名 test},
{ hello world}
} ;

var urlEncodedContent = new FormUrlEncodedContent(postData);
var httpClient = new HttpClient();
var result = httpClient.PostAsync(url,urlEncodedContent).Result.Content.ReadAsStringAsync()。Result;
Console.WriteLine(result);
}





但是在mvc控制器(作为服务器)中,如何接收名为postData的字典并返回客户端状态码?

解决方案

就MVC而言,方法名称可能是Say,它接受参数username,words。如果您缺少此映射,则无法从服务器端的客户端接收发布的数据。

为了更好地理解,请检查:样品

In the client I write the code like this:

static void Main(string[] args)
{
    var url = "http://www.cnblogs.com/api/say";

    var postData = new Dictionary<string, string>
    {
        { "username", "test" },
        { "words", "hello world" }
    };

    var urlEncodedContent = new FormUrlEncodedContent(postData);
    var httpClient = new HttpClient();
    var result = httpClient.PostAsync(url, urlEncodedContent).Result.Content.ReadAsStringAsync().Result;
    Console.WriteLine(result);
}



But in the mvc controller( as server),How to receieve the dictionary called postData and return the client statuscode?

解决方案

In terms of MVC, the method name might be "Say", which is accepting the parameters username, words. If you are missing this mapping then you cannot receive the posted data from client side on server side.
Just for better understanding, check this : Sample


这篇关于如何使用httpclient接收数据,我可以发送数据但不怎么收到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆